Using lambda while initializing member variables in class declaration

For readability, I like the initial member variables in the class declaration. I am also intrigued by using lambda during such initialization (see snippet). My question is what are the advantages or disadvantages of using such lambda functions in a declaration over member functions.

class foo { private: int index_ = 5; int value_ = [](int index) { int result = 0; for( int i = 0;i <index; i++) result += i; return result; }(index_ ); }; 

Edited: Since bar :: bar (int index): index_ (index) {} is more optimized than bar :: bar (int index) {index_ = index;}, does the above code work or is it just readability.

+5
source share

Source: https://habr.com/ru/post/1258458/


All Articles