public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/52688] New: static local variable can accessed from local class of function template
@ 2012-03-23 17:20 sir_nawaz959 at yahoo dot com
  2012-03-23 18:02 ` [Bug c++/52688] " giulianodammando at libero dot it
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: sir_nawaz959 at yahoo dot com @ 2012-03-23 17:20 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52688

             Bug #: 52688
           Summary: static local variable can accessed from local class of
                    function template
    Classification: Unclassified
           Product: gcc
           Version: 4.6.1
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: sir_nawaz959@yahoo.com


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'


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c++/52688] static local variable can accessed from local class of function template
  2012-03-23 17:20 [Bug c++/52688] New: static local variable can accessed from local class of function template sir_nawaz959 at yahoo dot com
@ 2012-03-23 18:02 ` giulianodammando at libero dot it
  2012-03-24 15:20 ` redi at gcc dot gnu.org
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: giulianodammando at libero dot it @ 2012-03-23 18:02 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52688

giulianodammando at libero dot it changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |giulianodammando at libero
                   |                            |dot it

--- Comment #1 from giulianodammando at libero dot it 2012-03-23 17:30:36 UTC ---
Confirmed on g++-mp-4.7


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c++/52688] static local variable can accessed from local class of function template
  2012-03-23 17:20 [Bug c++/52688] New: static local variable can accessed from local class of function template sir_nawaz959 at yahoo dot com
  2012-03-23 18:02 ` [Bug c++/52688] " giulianodammando at libero dot it
@ 2012-03-24 15:20 ` redi at gcc dot gnu.org
  2012-03-24 16:32 ` sir_nawaz959 at yahoo dot com
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: redi at gcc dot gnu.org @ 2012-03-24 15:20 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52688

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID
           Severity|major                       |normal

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-03-24 15:06:27 UTC ---
http://gcc.gnu.org/wiki/VerboseDiagnostics#missing_static_const_definition


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c++/52688] static local variable can accessed from local class of function template
  2012-03-23 17:20 [Bug c++/52688] New: static local variable can accessed from local class of function template sir_nawaz959 at yahoo dot com
  2012-03-23 18:02 ` [Bug c++/52688] " giulianodammando at libero dot it
  2012-03-24 15:20 ` redi at gcc dot gnu.org
@ 2012-03-24 16:32 ` sir_nawaz959 at yahoo dot com
  2012-03-25  4:27 ` redi at gcc dot gnu.org
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: sir_nawaz959 at yahoo dot com @ 2012-03-24 16:32 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52688

--- Comment #3 from Sarfaraz Nawaz <sir_nawaz959 at yahoo dot com> 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.


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c++/52688] static local variable can accessed from local class of function template
  2012-03-23 17:20 [Bug c++/52688] New: static local variable can accessed from local class of function template sir_nawaz959 at yahoo dot com
                   ` (2 preceding siblings ...)
  2012-03-24 16:32 ` sir_nawaz959 at yahoo dot com
@ 2012-03-25  4:27 ` redi at gcc dot gnu.org
  2012-03-26 15:44 ` redi at gcc dot gnu.org
                   ` (8 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: redi at gcc dot gnu.org @ 2012-03-25  4:27 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52688

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-03-25 02:56:09 UTC ---
Sorry, I didn't read the report carefully enough and was too hasty closing it.


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c++/52688] static local variable can accessed from local class of function template
  2012-03-23 17:20 [Bug c++/52688] New: static local variable can accessed from local class of function template sir_nawaz959 at yahoo dot com
                   ` (3 preceding siblings ...)
  2012-03-25  4:27 ` redi at gcc dot gnu.org
@ 2012-03-26 15:44 ` redi at gcc dot gnu.org
  2012-03-26 15:56 ` sir_nawaz959 at yahoo dot com
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: redi at gcc dot gnu.org @ 2012-03-26 15:44 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52688

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2012-03-26
     Ever Confirmed|0                           |1
      Known to fail|                            |4.1.2, 4.4.3, 4.5.2, 4.6.3,
                   |                            |4.7.0

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> 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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c++/52688] static local variable can accessed from local class of function template
  2012-03-23 17:20 [Bug c++/52688] New: static local variable can accessed from local class of function template sir_nawaz959 at yahoo dot com
                   ` (4 preceding siblings ...)
  2012-03-26 15:44 ` redi at gcc dot gnu.org
@ 2012-03-26 15:56 ` sir_nawaz959 at yahoo dot com
  2013-01-16  9:04 ` daniel.kruegler at googlemail dot com
                   ` (6 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: sir_nawaz959 at yahoo dot com @ 2012-03-26 15:56 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52688

--- Comment #6 from Sarfaraz Nawaz <sir_nawaz959 at yahoo dot com> 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

:-)


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c++/52688] static local variable can accessed from local class of function template
  2012-03-23 17:20 [Bug c++/52688] New: static local variable can accessed from local class of function template sir_nawaz959 at yahoo dot com
                   ` (5 preceding siblings ...)
  2012-03-26 15:56 ` sir_nawaz959 at yahoo dot com
@ 2013-01-16  9:04 ` daniel.kruegler at googlemail dot com
  2013-03-01 19:54 ` jared.cantwell at gmail dot com
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2013-01-16  9:04 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52688

Daniel Krügler <daniel.kruegler at googlemail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |daniel.kruegler at
                   |                            |googlemail dot com

--- Comment #7 from Daniel Krügler <daniel.kruegler at googlemail dot com> 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


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c++/52688] static local variable can accessed from local class of function template
  2012-03-23 17:20 [Bug c++/52688] New: static local variable can accessed from local class of function template sir_nawaz959 at yahoo dot com
                   ` (6 preceding siblings ...)
  2013-01-16  9:04 ` daniel.kruegler at googlemail dot com
@ 2013-03-01 19:54 ` jared.cantwell at gmail dot com
  2013-03-01 23:09 ` paolo.carlini at oracle dot com
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: jared.cantwell at gmail dot com @ 2013-03-01 19:54 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52688

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; };
    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.


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c++/52688] static local variable can accessed from local class of function template
  2012-03-23 17:20 [Bug c++/52688] New: static local variable can accessed from local class of function template sir_nawaz959 at yahoo dot com
                   ` (7 preceding siblings ...)
  2013-03-01 19:54 ` jared.cantwell at gmail dot com
@ 2013-03-01 23:09 ` paolo.carlini at oracle dot com
  2013-03-01 23:16 ` paolo.carlini at oracle dot com
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: paolo.carlini at oracle dot com @ 2013-03-01 23:09 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52688

--- Comment #9 from Paolo Carlini <paolo.carlini at oracle dot com> 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.


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c++/52688] static local variable can accessed from local class of function template
  2012-03-23 17:20 [Bug c++/52688] New: static local variable can accessed from local class of function template sir_nawaz959 at yahoo dot com
                   ` (8 preceding siblings ...)
  2013-03-01 23:09 ` paolo.carlini at oracle dot com
@ 2013-03-01 23:16 ` paolo.carlini at oracle dot com
  2013-03-01 23:19 ` jared.cantwell at gmail dot com
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: paolo.carlini at oracle dot com @ 2013-03-01 23:16 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52688

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
      Known to work|                            |4.8.0
         Resolution|                            |FIXED
   Target Milestone|---                         |4.8.0

--- Comment #10 from Paolo Carlini <paolo.carlini at oracle dot com> 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.


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c++/52688] static local variable can accessed from local class of function template
  2012-03-23 17:20 [Bug c++/52688] New: static local variable can accessed from local class of function template sir_nawaz959 at yahoo dot com
                   ` (9 preceding siblings ...)
  2013-03-01 23:16 ` paolo.carlini at oracle dot com
@ 2013-03-01 23:19 ` jared.cantwell at gmail dot com
  2013-03-01 23:24 ` paolo.carlini at oracle dot com
  2013-03-02 11:18 ` paolo at gcc dot gnu.org
  12 siblings, 0 replies; 14+ messages in thread
From: jared.cantwell at gmail dot com @ 2013-03-01 23:19 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52688

--- Comment #11 from jared.cantwell at gmail dot com 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.


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c++/52688] static local variable can accessed from local class of function template
  2012-03-23 17:20 [Bug c++/52688] New: static local variable can accessed from local class of function template sir_nawaz959 at yahoo dot com
                   ` (10 preceding siblings ...)
  2013-03-01 23:19 ` jared.cantwell at gmail dot com
@ 2013-03-01 23:24 ` paolo.carlini at oracle dot com
  2013-03-02 11:18 ` paolo at gcc dot gnu.org
  12 siblings, 0 replies; 14+ messages in thread
From: paolo.carlini at oracle dot com @ 2013-03-01 23:24 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52688

--- Comment #12 from Paolo Carlini <paolo.carlini at oracle dot com> 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.


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [Bug c++/52688] static local variable can accessed from local class of function template
  2012-03-23 17:20 [Bug c++/52688] New: static local variable can accessed from local class of function template sir_nawaz959 at yahoo dot com
                   ` (11 preceding siblings ...)
  2013-03-01 23:24 ` paolo.carlini at oracle dot com
@ 2013-03-02 11:18 ` paolo at gcc dot gnu.org
  12 siblings, 0 replies; 14+ messages in thread
From: paolo at gcc dot gnu.org @ 2013-03-02 11:18 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52688

--- Comment #13 from paolo at gcc dot gnu.org <paolo at gcc dot 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


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2013-03-02 11:18 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-23 17:20 [Bug c++/52688] New: static local variable can accessed from local class of function template sir_nawaz959 at yahoo dot com
2012-03-23 18:02 ` [Bug c++/52688] " giulianodammando at libero dot it
2012-03-24 15:20 ` redi at gcc dot gnu.org
2012-03-24 16:32 ` sir_nawaz959 at yahoo dot com
2012-03-25  4:27 ` redi at gcc dot gnu.org
2012-03-26 15:44 ` redi at gcc dot gnu.org
2012-03-26 15:56 ` sir_nawaz959 at yahoo dot com
2013-01-16  9:04 ` daniel.kruegler at googlemail dot com
2013-03-01 19:54 ` jared.cantwell at gmail dot com
2013-03-01 23:09 ` paolo.carlini at oracle dot com
2013-03-01 23:16 ` paolo.carlini at oracle dot com
2013-03-01 23:19 ` jared.cantwell at gmail dot com
2013-03-01 23:24 ` paolo.carlini at oracle dot com
2013-03-02 11:18 ` paolo at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).