[Bug c++/13262] New: "xxx is private within this context" when initializing a *self-contained* template class

fmonica at ce dot unipr dot it gcc-bugzilla@gcc.gnu.org
Mon Dec 1 17:03:00 GMT 2003


This code will compile successful with gcc <= 3.2.3 but doesn't work with gcc 
3.3.1 or 3.3.2: 
----------------------------------------------------- 
template < typename T > 
class Aclass 
  { 
  private: 
    Aclass() {} 
    static Aclass instance; 
  }; 
 
template < typename T > Aclass<T> Aclass<T>::instance; 
 
 
template class Aclass<int>; 
----------------------------------------------------- 
 
The gcc compiler emits an error like this: 
test.cc: In instantiation of `Aclass<int> Aclass<int>::instance': 
test.cc:12:   instantiated from here 
test.cc:5: error: `Aclass<T>::Aclass() [with T = int]' is private 
test.cc:9: error: within this context 
 
This code also works if an explicit instantiation of "instance" is added 
before the last line: 
Aclass<int> Aclass<int>::instance; 
 
That strategy (self contained class with private ctor) will be useful to build 
pattern like "Singleton", proposed by GoF. Of course there are some other ways 
to do this :-) 
 
I'm in doublt, but I think the code should be correct...

-- 
           Summary: "xxx is private within this context" when initializing a
                    *self-contained* template class
           Product: gcc
           Version: 3.3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: fmonica at ce dot unipr dot it
                CC: gcc-bugs at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13262



More information about the Gcc-bugs mailing list