Is there any difference in using "one-time" lambda in default initializer and using plain old member function?
struct A
{
int i;
int j = [&]
// something non-trivial,
// that requires multiple
// statements and depends
// on upper data members
{
int f = 0;
for (int k = 0; k < i; ++k) {
f += k;
}
return f;
}();
A(int k) : i(k) { ; }
};
Versus:
struct A
{
int i;
int J() const
{
int f = 0;
for (int k = 0; k < i; ++k) {
f += k;
}
return f;
}
int j = J();
A(int k) : i(k) { ; }
};
The only I see is the downside of the second approach: here extra symbol J introduced into namespace of class A. Are there another distinctions?
Regarding performance, there is no difference in the gcc 7.1 -O3 compiled code. Both implementations yield the same assembly.
The test code:
gets compiled to: