https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80935
Bug ID: 80935 Summary: [C++1z] incorrect error 'uninitialized variable in constexpr function' when conditionally declaring variable inside lambda inside template class Product: gcc Version: 7.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: vis...@royal-caliber.com Target Milestone: --- Created attachment 41445 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=41445&action=edit preprocessed file generated by g++ -std=c++1z -v -save-temps The following code fails to compile with g++ 7.1.0 with flags -std=c++1z. It compiles with -std=c++14 or -std=c++11. I believe expected behavior is to compile cleanly. clang-4.0 also has no problem with this code. struct S { int x; int y; }; template <class T> struct Foo { void foo() { auto f = [](int x) { if (x) { S s; s.x = 20; s.y = 10; (void) s; } }; f(10); } }; int main() { Foo<int> f; f.foo(); } When compiled with g++ -std=c++1z, the following error is raised: test.cpp: In instantiation of ‘Foo<T>::foo()::<lambda(int)> [with T = int]’: test.cpp:26:15: required from ‘struct Foo<T>::foo() [with T = int]::<lambda(int)>’ test.cpp:26:10: required from ‘void Foo<T>::foo() [with T = int]’ test.cpp:45:9: required from here test.cpp:30:11: error: uninitialized variable ‘s’ in ‘constexpr’ function S s; ^ test.cpp:14:8: note: ‘struct S’ has no user-provided default constructor struct S ^ test.cpp:16:7: note: and the implicitly-defined constructor does not initialize ‘int S::x’ int x; The gcc build information is as follows: Using built-in specs. COLLECT_GCC=g++-7.1 COLLECT_LTO_WRAPPER=/home/vishal/localbuild/libexec/gcc/x86_64-pc-linux-gnu/7.1.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: ./configure --prefix=/home/vishal/localbuild --enable-languages=c,c++ --program-suffix=-7.1 --disable-multilib Thread model: posix gcc version 7.1.0 (GCC) COLLECT_GCC_OPTIONS='-std=c++1z' '-Wall' '-Wextra' '-v' '-save-temps' '-shared-libgcc' '-mtune=generic' '-march=x86-64' /home/vishal/localbuild/libexec/gcc/x86_64-pc-linux-gnu/7.1.0/cc1plus -fpreprocessed test.ii -quiet -dumpbase test.cpp -mtune=generic -march=x86-64 -auxbase test -Wall -Wextra -std=c++1z -version -o test.s GNU C++14 (GCC) version 7.1.0 (x86_64-pc-linux-gnu) compiled by GNU C version 7.1.0, GMP version 6.1.0, MPFR version 3.1.3-p4, MPC version 1.0.2, isl version none GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 GNU C++14 (GCC) version 7.1.0 (x86_64-pc-linux-gnu) compiled by GNU C version 7.1.0, GMP version 6.1.0, MPFR version 3.1.3-p4, MPC version 1.0.2, isl version none GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072