13262 – [3.3/3.4 regression] "xxx is private within this context" when initializing a self-contained template class (original) (raw)

Description Francesco Monica 2003-12-01 17:02:56 UTC

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 Aclass::instance; template class Aclass;

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::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 Aclass::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...

Comment 1 Wolfgang Bangerth 2003-12-01 20:57:20 UTC

Confirmed. Kriang, you are the friendship expert: I am not quite sure what the standard says, but the present behavior doesn't really make much sense. What do you think?

W.

Comment 2 Drea Pinski 2003-12-02 00:45:08 UTC

From Phil's regression hunter: Search converges between 2003-05-15-trunk (#296) and 2003-05 -29-trunk (#297).

Comment 3 Drea Pinski 2003-12-02 00:51:50 UTC

I should say that date that I just referenced is for the mainline being broken again (it was fixed at one point between the time 3.3 branched and that date: Search converges between 2003-02-25- trunk (#231) and 2003-02-26-trunk (#232).).

It also was broken before the branch: Search converges between 2002-10-11-trunk (#101) and 2002-10-12-trunk (#102).

It was never fixed on the 3.3 branch though.

Comment 4 Kriang Lerdsuwanakij 2003-12-05 12:09:19 UTC

Yup, this should compile. Somewhere in deferred access checking code is broken.

Comment 8 Kriang Lerdsuwanakij 2003-12-18 14:31:17 UTC

Fixed in 3.3 and 3.4 branches.