By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,677 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,677 IT Pros & Developers. It's quick & easy.

private static member initialization

P: n/a
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*insta nce;*}
**};

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.
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Francesco Monica" <fm*****@ce.unipr.it> wrote...
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?
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
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.