This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/32534] gcc fails to initialize template's static data members before their use in some cases



------- 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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]