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++/63267] New: Static member of template class is not constructed before it's used.


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63267

            Bug ID: 63267
           Summary: Static member of template class is not constructed
                    before it's used.
           Product: gcc
           Version: 4.4.7
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yuxiao1985 at hotmail dot com

// test.cpp
#include <iostream>
using namespace std;

struct Output
{
    Output();
    void doIt() { cout<<"do it a:"<<a<<"\n";}
    int a;
};

Output::Output(): a(100)
{
    cout<<"Output constructed\n";
}

template <class T>
class Test
{
    public:
   static void use(const Output& ) { }
   static int callStaticMember();
   static Output out;
};

template <class T>
Output Test<T>::out;

template <class T>
int Test<T>::callStaticMember()
{
    use(out);
    out.doIt();
    return 0;
}

int a = Test<int>::callStaticMember();

int main()
{
    return 0;
}


The result is not correct. the static member should be constructed first.

----------------------------
[yxiao2@centos:~]$ g++ test.cpp
[yxiao2@centos:~]$ ./a.out
do it a:0
Output constructed


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