private static member initialization



 DEVELOP > c-Plus-Plus > private static member initialization

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Francesco Monica"
Date: 27 Nov 2003 06:09:45 AM
Object: private static member initialization
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.
.

User: "Victor Bazarov"

Title: Re: private static member initialization 27 Nov 2003 11:38:41 AM
"Francesco Monica" <fmonica@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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER