Bug 52688 - static local variable can accessed from local class of function template
Summary: static local variable can accessed from local class of function template
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.6.1
: P3 normal
Target Milestone: 4.8.0
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-03-23 16:56 UTC by Sarfaraz Nawaz
Modified: 2013-03-02 11:17 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 4.8.0
Known to fail: 4.1.2, 4.4.3, 4.5.2, 4.6.3, 4.7.0
Last reconfirmed: 2012-03-26 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sarfaraz Nawaz 2012-03-23 16:56:20 UTC
As per the C++ specification, static local variable can be accessed from local class of function template, but GCC 4.6.1 gives error for the following code-snippet:

template<typename T>
T f()
{
  static const double staticLocalVariable = 100.0;
  struct local
  {
      static double f() { return staticLocalVariable; }
  };
  return T(local::f());
}
 
int main() {
        std::cout << f<double>() << std::endl;
}

While gcc-4.5.1 compiles fine this code, gcc-4.6.1 gives error, saying:

undefined reference to `staticLocalVariable'
Comment 1 giulianodammando 2012-03-23 17:30:36 UTC
Confirmed on g++-mp-4.7
Comment 3 Sarfaraz Nawaz 2012-03-24 15:25:22 UTC
(In reply to comment #2)
> http://gcc.gnu.org/wiki/VerboseDiagnostics#missing_static_const_definition

Jonathan, that is a different case. A static (const or otherwise) member of a class needs a definition in order to take its address, but the bug I reported is not about static member of a class, rather it is about a static variable local to a function template. Interestingly as I said, this code compiles fine in gcc-4.5.1, but not in gcc-4.6.1. 


Also, if I do the same in a non-template function, it works fine, as expected.
Comment 4 Jonathan Wakely 2012-03-25 02:56:09 UTC
Sorry, I didn't read the report carefully enough and was too hasty closing it.
Comment 5 Jonathan Wakely 2012-03-26 15:31:20 UTC
(In reply to comment #0)
> While gcc-4.5.1 compiles fine this code

Are you sure? I get the same error with 4.1.2, 4.4.3, 4.5.2, 4.6.3 and 4.7.0

4.5.2 compiles and links OK with optimisation enabled, but so do the other versions
Comment 6 Sarfaraz Nawaz 2012-03-26 15:54:46 UTC
(In reply to comment #5)
> (In reply to comment #0)
> > While gcc-4.5.1 compiles fine this code
> 
> Are you sure? I get the same error with 4.1.2, 4.4.3, 4.5.2, 4.6.3 and 4.7.0
> 
> 4.5.2 compiles and links OK with optimisation enabled, but so do the other
> versions

I used ideone.com to compile it, which uses gcc-4.5.1 with -std=c++0x.

http://ideone.com/Y2vIF

I just noticed that though it compiles and links fine, but it doesn't print the expected output. :|

Interestingly, when I remove `const` from the definition of the static local variable, and then I attempt to change it from the local class, it then gives error:

http://ideone.com/vvwRc

:-)
Comment 7 Daniel Krügler 2013-01-16 09:03:56 UTC
I stumbled across a similar problem recently within a member function of a class template:

//----------------------------
template<class T>
struct A {
  static bool test() {
    static bool value = false;
    if (value)
      return false;
    struct S {
      S() { value = true; }
    };
    static S s;
    return true;
  }
};

int main()
{
  A<int>::test();
}
//----------------------------

"obj\Debug\main.o||In function `A<int>::test()::S::S()':|
main.cpp|8|undefined reference to `value'"

It doesn't occur if A is not a template.

I can confirm the error with gcc 4.7.2 and gcc 4.8.0 20130113 (experimental) compiled with the flags

-Wall -pedantic-errors
Comment 8 jared.cantwell 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; };
    bar();
}

int main()
{
    Foo<int>();
}
---------------------------------------------------
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()() const':
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.
Comment 9 Paolo Carlini 2013-03-01 23:08:40 UTC
(In reply to comment #8)
> 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; };
>     bar();
> }
> 
> int main()
> {
>     Foo<int>();
> }
> ---------------------------------------------------
> 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()()
> const':
> main.cpp:(.text+0x1a): undefined reference to `i'
> main.cpp:(.text+0x23): undefined reference to `i'
> collect2: error: ld returned 1 exit status

Can't be exactly the same issue: mainline and 4.7.3 get this one right.
Comment 10 Paolo Carlini 2013-03-01 23:15:22 UTC
In fact this issue (in terms of Comment #0 and Comment #7) is fixed for 4.8.0.
Comment 11 jared.cantwell 2013-03-01 23:18:23 UTC
(In reply to comment #9)
> (In reply to comment #8)
> > 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; };
> >     bar();
> > }
> > 
> > int main()
> > {
> >     Foo<int>();
> > }
> > ---------------------------------------------------
> > 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()()
> > const':
> > main.cpp:(.text+0x1a): undefined reference to `i'
> > main.cpp:(.text+0x23): undefined reference to `i'
> > collect2: error: ld returned 1 exit status
> 
> Can't be exactly the same issue: mainline and 4.7.3 get this one right.

It seems my comment is more aligned with http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54276 then this case.
Comment 12 Paolo Carlini 2013-03-01 23:23:40 UTC
Yes, it seems your own issue is a duplicate of PR54276, which luckily is already fixed both in mainline and for 4.7.3.
Comment 13 paolo@gcc.gnu.org 2013-03-02 11:17:48 UTC
Author: paolo
Date: Sat Mar  2 11:17:44 2013
New Revision: 196405

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=196405
Log:
2013-03-02  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/52688
	* g++.dg/template/static33.C: New.
	* g++.dg/template/static34.C: Likewise.

	PR c++/10291
	* g++.dg/template/static35.C: New.

Added:
    trunk/gcc/testsuite/g++.dg/template/static33.C
    trunk/gcc/testsuite/g++.dg/template/static34.C
    trunk/gcc/testsuite/g++.dg/template/static35.C
Modified:
    trunk/gcc/testsuite/ChangeLog