[Bug c++/53488] Incorrect code generated when capturing a constant by reference in a lambda

jpalecek at web dot de gcc-bugzilla@gcc.gnu.org
Sun Jun 24 20:23:00 GMT 2012


--- Comment #7 from Jiří Paleček <jpalecek at web dot de> 2012-06-24 20:23:38 UTC ---
Daniel, sorry, but I don't think your second example is relevant. First this
bug occurs only when capturing by reference, not by value, but your second
example uses capture by value. Moreover, while reading the standard, I can't
find anything that mandates the usage like yours to work. AFAIK it allows the
compiler to treat the lambda from your second example akin to this:

int foo() {
  const int i = 1;
  struct Lambda {
    const int i;
    Lambda(int i_) : i(i_) {}
    void operator()()const {
      int x[i];
  Lambda l(i);

which wouldn't work, and quite rightly. It couldn't even be modified to work,
as i must be static if it has to be both compile time constant and member of a

More information about the Gcc-bugs mailing list