Why internal struct template is accessible even if private?
Jonathan Wakely
jwakely.gcc@gmail.com
Sat May 7 08:57:00 GMT 2011
On 7 May 2011 03:42, Pavel Tolkachev wrote:
> Hi,
>
> I have seen that with the new compilers (last tried with g++ 4.4.5) nested struct templates are accessible from outside even when declared in the "private" section of a class but with older ones (like g++ 3.xx) they were not. I suspect new compilers are right but cannot trace it to the Standard -- any hints if it is the right behavior and how it follows from the Standard?
>
> The code I tried is below (I tried with -ansi and -std=c++98).
> --------cut here ---------
> #include<iostream>
> using namespace std;
>
> class S {
> private:
> template<typename T>
> struct I {
> static const T i;
> };
> };
>
> template<>
> const int S::I<int>::i = 5;
>
> class S0 {
> private:
> struct I {
> static const int i;
> };
> };
>
> const int S0::I::i = 6;
>
> int main(int, char *[]) {
> cout<< "S::I<int>::i="<< S::I<int>::i<< endl; // why does this compile?
> // cout<< "S0::I::i="<< S0::I::i<< endl; // this would not not compile, as expected, with this error msg (g++ 4.4.5):
> // tic.cpp:18: error: ‘struct S0::I’ is private
> // tic.cpp:28: error: within this context
> return 0;
> }
> ----------cut here------
This is a gcc bug, maybe this one:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40901
> Note: someone on comp.lang.c++ tested the code with Comeau C/C++ 4.3.10.1
> (obviously after renaming it to ComeauTest.c) and received this error:
No, they used the online Comeau compiler:
http://www.comeaucomputing.com/tryitout
> "ComeauTest.c", line 26: error: class template "S::I" (declared at line 7) is
> inaccessible
> cout<< "S::I<int>::i="<< S::I<int>::i<< endl; //why does this compile?
> ^
>
>
> Who is right, and if it's g++ 4.4.5, how does it follow from the Standard that the code must compile?
Comeau is right.
More information about the Gcc-help
mailing list