private static member initialization

F

Francesco Monica

Hello.

This (example) code will compile correctly with gcc <= 3.3.0, but doesn't
compile with gcc 3.3.1 (under linux):

-------------------------------------------------------------
template < typename T >
class Singleton
  {
  private:
    Singleton() {}
    static Singleton instance;

  public:
    static Singleton& getInstance() { return instance; }
  };

template < typename T > Singleton<T> Singleton<T>::instance;

Singleton<int>& a = Singleton<int>::getInstance();
--------------------------------------------------------------

This is the error returned by gcc:

test.cc: In instantiation of `Singleton<int> Singleton<int>::instance':
test.cc:9:   instantiated from `static Singleton<T>& Singleton<T>::getInstance() [with T = int]'
test.cc:15:   instantiated from here
test.cc:5: error: `Singleton<T>::Singleton() [with T = int]' is private
test.cc:12: error: within this context

Btw, the code also works with the "Test Drive Comeau C++ Online"...

Did I make some errors?
Is correct to initialize private data members in that manner?
(With a non-template "Singleton" no errors were produced)

I wish to test the code with the new gcc 3.3.2, but I have no ways to
install it by now...

Can you give me some suggestions?

Thank you in advance.
Francesco.
 
V

Victor Bazarov

Francesco Monica said:
This (example) code will compile correctly with gcc <= 3.3.0, but doesn't
compile with gcc 3.3.1 (under linux):

-------------------------------------------------------------
template < typename T >
class Singleton
{
private:
Singleton() {}
static Singleton instance;

public:
static Singleton& getInstance() { return instance; }
};

template < typename T > Singleton<T> Singleton<T>::instance;

Singleton<int>& a = Singleton<int>::getInstance();
--------------------------------------------------------------

This is the error returned by gcc:

test.cc: In instantiation of `Singleton<int> Singleton<int>::instance':
test.cc:9: instantiated from `static Singleton<T>&
Singleton said:
test.cc:15: instantiated from here
test.cc:5: error: `Singleton<T>::Singleton() [with T = int]' is private
test.cc:12: error: within this context

Btw, the code also works with the "Test Drive Comeau C++ Online"...

Did I make some errors?

Not that I can see.
Is correct to initialize private data members in that manner?
(With a non-template "Singleton" no errors were produced)


It's fine.
I wish to test the code with the new gcc 3.3.2, but I have no ways to
install it by now...

I believe some readers of this newsgroup can help, I am not one of
them, unfortunately. Have you tried gnu.g++.* newsgroups?
Can you give me some suggestions?

Contact the GNU GCC Project, they may have an idea. It does look
like a regression from the earlier version.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top