[Bug c++/52688] static local variable can accessed from local class of function template

jared.cantwell at gmail dot com gcc-bugzilla@gcc.gnu.org
Fri Mar 1 19:54:00 GMT 2013


jared.cantwell at gmail dot com changed:

           What    |Removed                     |Added
                 CC|                            |jared.cantwell at gmail dot
                   |                            |com

--- Comment #8 from jared.cantwell at gmail dot com 2013-03-01 19:53:56 UTC ---
I just came across the same issue in the context of lambdas (for posterity,
since I spent time thinking this was specific to lambdas).

Consider the following source code:
template<typename T>
void Foo()
    static int i = 5;
    auto bar = [&]() { ++i; };

int main()
GCC 4.7.0 produces the following error:

# g++-4.7 -std=c++11 main.cpp
/tmp/ccbZwBzT.o: In function `void Foo<int>()::{lambda()#1}::operator()()
main.cpp:(.text+0x1a): undefined reference to `i'
main.cpp:(.text+0x23): undefined reference to `i'
collect2: error: ld returned 1 exit status

However, when Foo is not a template there is no error.

More information about the Gcc-bugs mailing list