http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58062
Bug ID: 58062 Summary: [C++11] bogus __func__ lookup in lambda body Product: gcc Version: 4.8.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: hstong at ca dot ibm.com In the case below, the __func__ identifier appears within a lambda body at namespace scope. GCC seems to find __func__ from somewhere, but the behaviour does not appear to be supported by the Standard. MSVC gives the expected error. When we are told by C++11 subclause 5.1.2 [expr.prim.lambda] paragraph 7 that the compound-statement "yields" the function-body of the function call operator, it can be taken that it means that a function-body is produced from the compound-statement and that the latter is not an actual function-body. We are also told that for the purposes of name lookup, the compound-statement is considered in the context of the lambda-expression. We find that, in the absence of a function-body, __func__ is not a specified to be a predefined variable (8.4.1 [dcl.fct.def.general] paragraphs 7-8). We also note that the form of a function definition (8.4.1 paragraph 1) is not present in the above program and that the wording in subclauses 3.3.2 [basic.scope.pdecl] and 3.3.3 [basic.scope.block] (paragraphs 8 and 2, respectively) covers only function-local predefined variables in function definitions. The conclusion is that __func__ in a lambda body is bound using the context of the lambda-expression, and not bound later to be the function-local predefined variable which would exist in the context of the function-call operator's compiler-generated definition. In the case of the code below, it means that the lookup for __func__ fails and renders the program ill-formed. ## Small test: namespace K { auto ff = [] { return __func__; }; } ## Actual output: Clean compile. ## Expected output: main.cpp: In lambda function: main.cpp:2:26: error: ‘__func__’ was not declared in this scope auto ff = [] { return __func__; }; ^ ## g++ -v: Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.1-2ubuntu1~12.04' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.8.1 (Ubuntu 4.8.1-2ubuntu1~12.04)