Why internal struct template is accessible even if private?

Discussion in 'C++' started by Pavel, May 1, 2011.

  1. Pavel

    Pavel Guest

    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 is below:

    --------cut here ---------
    #include <iostream>
    using namespace std;

    class S {
    private:
    template <typename T>
    struct I
    {
    const static T i;
    };
    };

    template <>
    const int S::I<int>::i = 5;

    class S0 {
    private:
    struct I {
    const static 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------

    Thanks in advance,
    -Pavel
     
    Pavel, May 1, 2011
    #1
    1. Advertising

  2. * Pavel, on 01.05.2011 01:03:
    > #include <iostream>
    > using namespace std;
    >
    > class S {
    > private:
    > template <typename T>
    > struct I
    > {
    > const static T i;
    > };
    > };
    >
    > template <>
    > const int S::I<int>::i = 5;
    >
    > class S0 {
    > private:
    > struct I {
    > const static 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;
    > }


    perhaps you forgot to use proper options to make compilers conforming

    <comeau>
    Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
    Copyright 1988-2008 Comeau Computing. All rights reserved.
    MODE:strict errors C++ C++0x_extensions

    "ComeauTest.c", line 9: warning: storage class is not first
    const static T i;
    ^

    "ComeauTest.c", line 19: warning: storage class is not first
    const static int i;
    ^

    "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?
    ^

    1 error detected in the compilation of "ComeauTest.c".
    </comeau>


    cheers & hth.,

    - Alf


    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, May 1, 2011
    #2
    1. Advertising

  3. Pavel

    Pavel Guest

    Alf P. Steinbach /Usenet wrote:
    > * Pavel, on 01.05.2011 01:03:
    >> #include <iostream>
    >> using namespace std;
    >>
    >> class S {
    >> private:
    >> template <typename T>
    >> struct I
    >> {
    >> const static T i;
    >> };
    >> };
    >>
    >> template <>
    >> const int S::I<int>::i = 5;
    >>
    >> class S0 {
    >> private:
    >> struct I {
    >> const static 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;
    >> }

    >
    > perhaps you forgot to use proper options to make compilers conforming
    >
    > <comeau>
    > Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
    > Copyright 1988-2008 Comeau Computing. All rights reserved.
    > MODE:strict errors C++ C++0x_extensions
    >
    > "ComeauTest.c", line 9: warning: storage class is not first
    > const static T i;
    > ^
    >
    > "ComeauTest.c", line 19: warning: storage class is not first
    > const static int i;
    > ^
    >
    > "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?
    > ^
    >
    > 1 error detected in the compilation of "ComeauTest.c".
    > </comeau>

    Thanks Alf!

    First I forgot but now I tried it with -ansi and -std=c++98 which both stand for
    "The 1998 ISO C++ standard plus amendments" and it still compiles. Now that I
    know Comeau does not like it, I will take it with gnu.g++.bug.

    If other people could try it with their compilers and report the results I would
    appreciate that.

    -Pavel

    >
    >
    > cheers & hth.,
    >
    > - Alf
    >
    >
     
    Pavel, May 1, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jack Wright
    Replies:
    3
    Views:
    1,632
    Chris Jackson
    Jan 5, 2004
  2. Tim Zych
    Replies:
    3
    Views:
    455
    Tim Zych
    May 12, 2004
  3. Richard
    Replies:
    1
    Views:
    1,796
    Steven Cheng[MSFT]
    Jul 27, 2004
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,002
    Smokey Grindel
    Dec 2, 2006
  5. Ajay
    Replies:
    29
    Views:
    575
    Chris Dollin
    Aug 4, 2008
Loading...

Share This Page