[Bug c++/106864] Unexpected capture of "constexpr int" variable inside lambda

pinskia at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Fri Oct 21 03:31:45 GMT 2022


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106864

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Take:
void bad() {
  constexpr int x = 123;
  auto Outer = [&] [[gnu::noipa]] () {
    auto L = [=]  [[gnu::noipa]] () {
      const int *x1 = &x;
      for (int i = 0; i < *x1; ++i) { asm("":::"memory"); }
    };
    asm("":::"memory");
   L();
  };
  Outer();
}

void good() {
  constexpr int x = 123;
  auto L = [=] [[gnu::noipa]]() {
      const int *x1 = &x;
    for (int i = 0; i < *x1; ++i) {asm("":::"memory"); }    
  };
  L();
}

In the bad case, L is sizeof int and Outer is still 1. In good, it is sizeof
int.
This is for GCC and clang.

So I think GCC is just capturing and not removing constexpr after doing the
prop for constexpr. Which I think is just a missed optimization.


More information about the Gcc-bugs mailing list