What is the compiler allowed to omit from by-value default captures, when only some data members of an implicitly captured object are actually used by the functor? E.g.,
struct A {
// some members we care about:
char x;
int y;
// some huge amount of state we do not:
std::array<bool, 200000> z;
int foo() const { return y + 1 }
};
void bar() {
A a;
// must the entirety of a be copy captured, or is the compiler allowed to pick/prune?
auto l1 = [=](){ std::cout << a.x << ", " << a.y << std::endl; };
// ...
}
Similarly, when if ever is early evaluation allowed to omit broader captures?
void baz(int i) {
A a2;
a2.y = i;
// capture fundamentally only needs 1 int, not all of an A instance.
auto l2 = [=](){ std::cout << a.foo() << std::endl; }
}
There are at least some situations where making a partial vs. complete copy capture of an element should have no visible external effects beyond lambda size, but I do not know where in the spec to look for the answer to what optimizations are allowable.
ais captured, the compiler only has to ensure that the output of the program matches the theoretical output, so it could decide to not copya.z.Ahad a non-default copy constructor, that copy constructor must be invoked. If the compiler can prove that after executing the constructor it is not necessary to trivially-copy some other members of the captured class, then the compiler is not required to go through the motions of actually doing so. But it still must invoke the copy-constructor "as if" the entire instance was captured by value.std::array<bool,N>doesn't have any visible side-effects