Bug 54801 - [4.7/4.8 Regression] static variables constructed with lambda params inside member functions cause undefined errors
Summary: [4.7/4.8 Regression] static variables constructed with lambda params inside m...
Status: RESOLVED DUPLICATE of bug 55015
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.7.2
: P2 normal
Target Milestone: 4.7.3
Assignee: Not yet assigned to anyone
URL:
Keywords: c++-lambda
Depends on:
Blocks: lambdas
  Show dependency treegraph
 
Reported: 2012-10-03 22:27 UTC by Matt Godbolt
Modified: 2022-03-11 00:32 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2012-10-04 00:00:00


Attachments
Test C file exhibiting the issue (215 bytes, text/x-c++src)
2012-10-03 22:27 UTC, Matt Godbolt
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Matt Godbolt 2012-10-03 22:27:53 UTC
Created attachment 28346 [details]
Test C file exhibiting the issue

In the attached code, there is static method with a static variable whose constructor takes a function pointer.  When a lambda is passed as the function pointer, it seems the lambda's definition is dropped and the result is a link-time error.

$ g++ -o test -std=c++0x test.cc
/tmp/ccpBFtG7.o: In function `Test::aFunc()::{lambda(int*)#1}::operator void (*)(int*)() const':
test.cc:(.text._ZZN4Test5aFuncEvENKUlPiE_cvPFvS0_EEv[_ZZN4Test5aFuncEvENKUlPiE_cvPFvS0_EEv]+0x9): undefined reference to `Test::aFunc()::{lambda(int*)#1}::_FUN(int*)'
collect2: error: ld returned 1 exit status

Making the variable non-static fixes the issue.  The same code in a free function does not exhibit the problem.  Taking a std::function<void(int*)> as the constructor parameter also cures the issue.  It seems to be something which only throws away the lambda for static variables inside member functions.

More info:

g++ -v
Using built-in specs.
COLLECT_GCC=/site/apps/gcc-4.7.2-drw.patched.1/bin/g++
COLLECT_LTO_WRAPPER=/site/apps/gcc-4.7.2-drw.patched.1/libexec/gcc/x86_64-linux-gnu/4.7.2/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ./configure --prefix /site/apps/gcc-4.7.2-drw.patched.1 --build=x86_64-linux-gnu --enable-clocale=gnu --enable-gold --enable-languages=c,c++ --enable-ld=default --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-linker-build-id --enable-lto --enable-plugin --enable-shared --enable-threads=posix --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-pkgversion=DRW-internal-build --with-plugin-ld=ld.gold --with-system-zlib --with-gmp=/data/teamcity/work/sup-chietfbld02-002/8a981abc13d5c0c5/scratch/gcc/4.7.2/build/gmp-5.0.2 --with-mpfr=/data/teamcity/work/sup-chietfbld02-002/8a981abc13d5c0c5/scratch/gcc/4.7.2/build/mpfr-3.1.1 --with-mpc=/data/teamcity/work/sup-chietfbld02-002/8a981abc13d5c0c5/scratch/gcc/4.7.2/build/mpc-0.9 --with-libelf=/data/teamcity/work/sup-chietfbld02-002/8a981abc13d5c0c5/scratch/gcc/4.7.2/build/libelf-0.8.9
Thread model: posix
gcc version 4.7.2 (DRW-internal-build) 

This issue was discovered when attempting to register lambda functions as the "cleanup function" for static thread-local stored information using boost::thread_specific_ptr. However, the issue is reproducible in the attached source file with no external includes.

This appears to be a regression in 4.7.2: gcc versions 4.7.1, 4.7.0, 4.6.3 and 4.5.3 compile the source without issue.
Comment 1 Daniel Krügler 2012-10-04 07:11:14 UTC
Also occurs on gcc 4.8.0 20120930 (experimental).
Comment 2 Paolo Carlini 2012-10-04 09:26:55 UTC
Confirmed. Maybe H.J can help.
Comment 4 Paolo Carlini 2012-10-04 11:41:06 UTC
Thanks Jakub. Let's add Jason in CC then.
Comment 5 Paolo Carlini 2012-12-06 15:44:50 UTC
Dup.

*** This bug has been marked as a duplicate of bug 55015 ***