public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/12222] New: explicit initialization of static template members seems ignored
@ 2003-09-09 10:16 stefaandr at hotmail dot com
  2003-09-09 14:08 ` [Bug c++/12222] " bangerth at dealii dot org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: stefaandr at hotmail dot com @ 2003-09-09 10:16 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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

           Summary: explicit initialization of static template members seems
                    ignored
           Product: gcc
           Version: 3.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: stefaandr at hotmail dot com
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu

Shouldn't this code output 1234?  It outputs 0 for me.  I used to write
b a<float>::b1 instead of template <> b a<float>::b1, but
I tried this after seeing comment 3 on bug 11930.  I can even
change the constructor of b into a non-default one and it still compiles...

using g++-3.4 (GCC) 3.4 20030907 (experimental)

#include <iostream>
struct b {
  b() { m = 1234; }; int m;
};
template <class T>
  struct a { static b b1; };
template struct a<float>;
template <> b a<float>::b1;
int main() {
  std::cout << a<float>::b1.m << std::endl;
};


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

* [Bug c++/12222] explicit initialization of static template members seems ignored
  2003-09-09 10:16 [Bug c++/12222] New: explicit initialization of static template members seems ignored stefaandr at hotmail dot com
@ 2003-09-09 14:08 ` bangerth at dealii dot org
  2003-09-09 17:53 ` stefaandr at hotmail dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: bangerth at dealii dot org @ 2003-09-09 14:08 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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


bangerth at dealii dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|                            |1
   Last reconfirmed|0000-00-00 00:00:00         |2003-09-09 14:08:31
               date|                            |


------- Additional Comments From bangerth at dealii dot org  2003-09-09 14:08 -------
Your code is invalid: you have the explicit instantiation before the declaration 
of an explicit specialization of the member. That confuses the compiler. If you 
revert the order, you get this error message: 
 
tmp/g> ../build-gcc/gcc-install/bin/c++ x.cc -W -Wall 
/tmp/ccQIMlpO.o(.text+0x11): In function `main': 
: undefined reference to `a<float>::b1' 
collect2: ld returned 1 exit status 
 
That is due to the fact that you only _declare_ the existence of an explicit specialization, 
not define it. If instead of 
  template <> b a<float>::b1; 
you write 
  template <> b a<float>::b1 = b(); 
then the code compiles properly and yields the expected result. 
 
Now, this is indeed confusing -- gcc should be able to issue a warning about specializing 
after instantiating. Besides, there is kind of a bug anyway: when accessing  
a<float>::b1.m, gcc is apparently presently using the static member from the general 
template, not the specialization. That's fine, because the code is invalid. However, it 
is equally apparently not initializing it properly, since otherwise we would get the 
right answer. 
 
W.


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

* [Bug c++/12222] explicit initialization of static template members seems ignored
  2003-09-09 10:16 [Bug c++/12222] New: explicit initialization of static template members seems ignored stefaandr at hotmail dot com
  2003-09-09 14:08 ` [Bug c++/12222] " bangerth at dealii dot org
@ 2003-09-09 17:53 ` stefaandr at hotmail dot com
  2003-09-09 18:57 ` bangerth at dealii dot org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: stefaandr at hotmail dot com @ 2003-09-09 17:53 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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



------- Additional Comments From stefaandr at hotmail dot com  2003-09-09 17:53 -------
Appearantly, if I do a specialized initialization
  template <> b a<float>::b1 = b();
and a templated one
  template <class T> b a<T>::b1 = b();
over different object files, the result of the linking is that the same object
(same this pointer) is initialized twice (seen by std-outing something inside
the constructor).  Is this a bug in the linker or the compiler?


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

* [Bug c++/12222] explicit initialization of static template members seems ignored
  2003-09-09 10:16 [Bug c++/12222] New: explicit initialization of static template members seems ignored stefaandr at hotmail dot com
  2003-09-09 14:08 ` [Bug c++/12222] " bangerth at dealii dot org
  2003-09-09 17:53 ` stefaandr at hotmail dot com
@ 2003-09-09 18:57 ` bangerth at dealii dot org
  2003-12-26 23:36 ` pinskia at gcc dot gnu dot org
  2004-08-16 18:37 ` bangerth at dealii dot org
  4 siblings, 0 replies; 6+ messages in thread
From: bangerth at dealii dot org @ 2003-09-09 18:57 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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



------- Additional Comments From bangerth at dealii dot org  2003-09-09 18:57 -------
That's your problem -- declaring an explicit specialization in a place where the 
compiler can't see it invokes undefined behavior. 
 
W.


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

* [Bug c++/12222] explicit initialization of static template members seems ignored
  2003-09-09 10:16 [Bug c++/12222] New: explicit initialization of static template members seems ignored stefaandr at hotmail dot com
                   ` (2 preceding siblings ...)
  2003-09-09 18:57 ` bangerth at dealii dot org
@ 2003-12-26 23:36 ` pinskia at gcc dot gnu dot org
  2004-08-16 18:37 ` bangerth at dealii dot org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2003-12-26 23:36 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2003-12-26 23:17 -------
It might be that the C++ standard does not require a diagnostic of the problem.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |accepts-invalid


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


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

* [Bug c++/12222] explicit initialization of static template members seems ignored
  2003-09-09 10:16 [Bug c++/12222] New: explicit initialization of static template members seems ignored stefaandr at hotmail dot com
                   ` (3 preceding siblings ...)
  2003-12-26 23:36 ` pinskia at gcc dot gnu dot org
@ 2004-08-16 18:37 ` bangerth at dealii dot org
  4 siblings, 0 replies; 6+ messages in thread
From: bangerth at dealii dot org @ 2004-08-16 18:37 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2004-08-16 18:37 -------
gcc's behavior actually makes a lot more sense than anyone seems to 
have noticed: with this code 
---------------- 
template <typename T> 
struct a { static int b1; }; 
 
template struct a<float>; 
template <> int a<float>::b1; 
 
int x = a<float>::b1; 
----------------- 
we indeed get no warning that a specialization is declared after  
instantiating the template. However, that's about ok: we haven't 
provided a definition of the static member, so it isn't instantiated 
and the declaration of the specialization doesn't come too late. 
 
On the other hand, for this code (not the presence of the definition) 
------------------ 
template <typename T> 
struct a { static int b1; }; 
template <typename T> int a<T>::b1; 
 
template struct a<float>; 
template <> int a<float>::b1; 
 
int x = a<float>::b1; 
------------------ 
we _do_ get an error: 
 
g/x> /home/bangerth/bin/gcc-3.5-pre/bin/c++ -Wall -c x.cc 
x.cc:6: error: redefinition of `int a<float>::b1' 
x.cc:3: error: `int a<float>::b1' previously declared here 
 
I think gcc is correct here. So closing. 
 
W. 

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


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


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

end of thread, other threads:[~2004-08-16 18:37 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-09 10:16 [Bug c++/12222] New: explicit initialization of static template members seems ignored stefaandr at hotmail dot com
2003-09-09 14:08 ` [Bug c++/12222] " bangerth at dealii dot org
2003-09-09 17:53 ` stefaandr at hotmail dot com
2003-09-09 18:57 ` bangerth at dealii dot org
2003-12-26 23:36 ` pinskia at gcc dot gnu dot org
2004-08-16 18:37 ` bangerth at dealii dot 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).