public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/32534]  New: gcc fails to initialize template's static data members before their use in some cases
@ 2007-06-28 10:36 vlbel at mail dot ru
  2009-06-11 16:34 ` [Bug c++/32534] " rodolfo at rodsoft dot org
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: vlbel at mail dot ru @ 2007-06-28 10:36 UTC (permalink / raw)
  To: gcc-bugs

the following programm
-----------------------------------------------
#include <iostream>

struct A
{
        A() : value(0) {}
        int value;
};

template<class T>
struct B
{
        static A a;
};

template<class T> A B<T>::a;
//template<> A B<int>::a;

template<class T>
struct C
{
        C() { B<int>::a.value = 1; }
};

C<float> c;

main()
{
        std::cout << B<int>::a.value << std::endl;
}

-------------------------------------------------
prints 0 instead of expected 1.
It is important that C is template. Otherwise (if C is ordinary class) the
output is 1.
If I use "template<> A B<int>::a;" instead of "template<class T> A B<T>::a;" I
got linker error: undefined reference to 'B<int>::a'.
I can't use "template<> A B<int>::a = something;" form (which would help)
because I have only empty ctor (like in the case of map).


-- 
           Summary: gcc fails to initialize template's static data members
                    before their use in some cases
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: vlbel at mail dot ru


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


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

* [Bug c++/32534] gcc fails to initialize template's static data members before their use in some cases
  2007-06-28 10:36 [Bug c++/32534] New: gcc fails to initialize template's static data members before their use in some cases vlbel at mail dot ru
@ 2009-06-11 16:34 ` rodolfo at rodsoft dot org
  2009-06-11 20:03 ` mikpe at it dot uu dot se
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: rodolfo at rodsoft dot org @ 2009-06-11 16:34 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from rodolfo at rodsoft dot org  2009-06-11 16:34 -------
I've been bitten by this bug, which is almost 2 years old. I haven't tested it
with gcc 4.4 though, but I confirm that it happens with gcc-4.3.3. Is there
anyone willing to correct this?


-- 


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


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

* [Bug c++/32534] gcc fails to initialize template's static data members before their use in some cases
  2007-06-28 10:36 [Bug c++/32534] New: gcc fails to initialize template's static data members before their use in some cases vlbel at mail dot ru
  2009-06-11 16:34 ` [Bug c++/32534] " rodolfo at rodsoft dot org
@ 2009-06-11 20:03 ` mikpe at it dot uu dot se
  2009-06-15  9:20 ` jwakely dot gcc at gmail dot com
  2009-06-15  9:55 ` jwakely dot gcc at gmail dot com
  3 siblings, 0 replies; 7+ messages in thread
From: mikpe at it dot uu dot se @ 2009-06-11 20:03 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from mikpe at it dot uu dot se  2009-06-11 20:03 -------
(In reply to comment #1)
> I've been bitten by this bug, which is almost 2 years old. I haven't tested it
> with gcc 4.4 though, but I confirm that it happens with gcc-4.3.3. Is there
> anyone willing to correct this?

gcc-4.4.1 20090609 also prints 0 (has the bug).


-- 


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


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

* [Bug c++/32534] gcc fails to initialize template's static data members before their use in some cases
  2007-06-28 10:36 [Bug c++/32534] New: gcc fails to initialize template's static data members before their use in some cases vlbel at mail dot ru
  2009-06-11 16:34 ` [Bug c++/32534] " rodolfo at rodsoft dot org
  2009-06-11 20:03 ` mikpe at it dot uu dot se
@ 2009-06-15  9:20 ` jwakely dot gcc at gmail dot com
  2009-06-15  9:55 ` jwakely dot gcc at gmail dot com
  3 siblings, 0 replies; 7+ messages in thread
From: jwakely dot gcc at gmail dot com @ 2009-06-15  9:20 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from jwakely dot gcc at gmail dot com  2009-06-15 09:19 -------
(In reply to comment #0)
> I can't use "template<> A B<int>::a = something;" form (which would help)
> because I have only empty ctor (like in the case of map).

I'm not sure what you mean but this works fine:

template<> A B<int>::a = A();




-- 

jwakely dot gcc at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jwakely dot gcc at gmail dot
                   |                            |com


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


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

* [Bug c++/32534] gcc fails to initialize template's static data members before their use in some cases
  2007-06-28 10:36 [Bug c++/32534] New: gcc fails to initialize template's static data members before their use in some cases vlbel at mail dot ru
                   ` (2 preceding siblings ...)
  2009-06-15  9:20 ` jwakely dot gcc at gmail dot com
@ 2009-06-15  9:55 ` jwakely dot gcc at gmail dot com
  3 siblings, 0 replies; 7+ messages in thread
From: jwakely dot gcc at gmail dot com @ 2009-06-15  9:55 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from jwakely dot gcc at gmail dot com  2009-06-15 09:55 -------
extern "C" int printf(const char*, ...);

struct A
{
        A() : value(1) { printf("A::A %d\n", value); }
        int value;
};

template<class T>
struct B
{
        static A a;
};

template<class T> A B<T>::a = A();

template<class T>
struct C
{
        C()
        {
            printf("C::C %d\n", B<int>::a.value);
            B<int>::a.value = 2;
        }
};

C<float> c;

int main()
{
    printf("main %d\n", B<int>::a.value);
}

compiled with 4.3.2 prints:
C::C 0
A::A 1
main 1

but I expect:
A::A 1
C::C 1
main 2

It seems that the initializer for B<T>::a runs after the initialization of c,
so although the B<int>::a.value gets set by C::C it then gets overwritten by
A::A


-- 


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


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

* [Bug c++/32534] gcc fails to initialize template's static data members before their use in some cases
       [not found] <bug-32534-4@http.gcc.gnu.org/bugzilla/>
  2011-11-03 18:28 ` richard-gccbugzilla at metafoo dot co.uk
@ 2011-11-03 18:47 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2011-11-03 18:47 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-11-03 18:47:32 UTC ---
Indeed, [basic.start.init] p2 says so, thanks.

An ordering could be enforced by explicitly specializing the static data
member:
  template<> A B<int>::a = A();
or in C++11
  template<> A B<int>::a{};

Or in C++11 by making A's constructor constexpr


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

* [Bug c++/32534] gcc fails to initialize template's static data members before their use in some cases
       [not found] <bug-32534-4@http.gcc.gnu.org/bugzilla/>
@ 2011-11-03 18:28 ` richard-gccbugzilla at metafoo dot co.uk
  2011-11-03 18:47 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 7+ messages in thread
From: richard-gccbugzilla at metafoo dot co.uk @ 2011-11-03 18:28 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Smith <richard-gccbugzilla at metafoo dot co.uk> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |richard-gccbugzilla at
                   |                            |metafoo dot co.uk

--- Comment #5 from Richard Smith <richard-gccbugzilla at metafoo dot co.uk> 2011-11-03 18:26:57 UTC ---
This does not look like a valid bug: static data members of class template
instantiations have unordered initialization, so gcc is permitted to initialize
B<T>::a after it initializes c.


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

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

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-28 10:36 [Bug c++/32534] New: gcc fails to initialize template's static data members before their use in some cases vlbel at mail dot ru
2009-06-11 16:34 ` [Bug c++/32534] " rodolfo at rodsoft dot org
2009-06-11 20:03 ` mikpe at it dot uu dot se
2009-06-15  9:20 ` jwakely dot gcc at gmail dot com
2009-06-15  9:55 ` jwakely dot gcc at gmail dot com
     [not found] <bug-32534-4@http.gcc.gnu.org/bugzilla/>
2011-11-03 18:28 ` richard-gccbugzilla at metafoo dot co.uk
2011-11-03 18:47 ` redi 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).