Accessing members of an outer class inside nested class since c++11

"Declarations in a nested class can use any members of the enclosing class, following the usual usage rules for the non-static members."
I've read it here at cppreference
(By the way, I can't find this rule in c++14 standard in chapter 11.7)

cppreference provides only 1 example of what was not possible to access from nested class before c++11 standard and what has become possible to access since c++11 standard - sizeof(name of member of the outer class).

Could please anybody provide some additional (without sizeof) example illustrating this access rule?

Perhaps:
1
2
3
4
5
6
7
8
9
class outer
{
    int x, y;

    struct inner
    {
        decltype(x) z;   // inner::z takes on the type of outer::x
    } ;
};
ISO/IEC 14882:2011(E)
11.7 Nested classes [class.access.nest]

1 A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules (Clause 11) shall be obeyed.

[Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class E {

    int x;

    class B { };

    class I {

        B b; // OK: E::I can access E::B

        int y;

        void f(E* p, int i) {

            p->x = i; // OK: E::I can access E::x
        }
    };

    int g(I* p) {

        return p->y; // error: I::y is private
    }
};

—end example ]


(Retained verbatim in C++14)
@cire
Thanks, very nice example.

@JLBorges
this example (without int g(I* p) function) compiles both with and without -std=c++11 option(gcc and clang).

My point is - has become possible something that was not before c++11?

For example: can private int x member from the example be accessed by any way other than
a) declaring it static or
b) through an object of the outer class?
Has changed the answer to this question since c++11?



Last edited on
> My point is - has become possible something that was not before c++11?

Yes.

ISO/IEC 14882:1998(E) (C++98):

11.8 Nested classes [class.access.nest]

1 The members of a nested class have no special access to members of an enclosing class, nor to classes or functions that have granted friendship to an enclosing class; the usual access rules (clause 11) shall be obeyed. The members of an enclosing class have no special access to members of a nested class; the usual access rules (clause 11) shall be obeyed.

[Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class E {

    int x;

    class B { };

    class I {

        B b; // error: E::B is private

        int y;

        void f(E* p, int i) {

            p->x = i; // error: E::x is private
        }
    };

    int g(I* p) {

        return p->y; // error: I::y is private
    }
};

—end example ]

2 [Note: because a base-clause for a nested class is part of the declaration of the nested class itself (and not part of the declarations of the members of the nested class), the base-clause may refer to the private members of the enclosing class. For example,
1
2
3
4
5
6
7
8
9
10
11
12
13
class C {

    class A { };
    A *p;  // OK

    class B : A  // OK
    {  
        A  *q;  // OK because of injection of name A in A
        C::A *r;  // error, C::A is inaccessible
        B  *s;  // OK because of injection of name B in B
        C::B *t;  // error, C::B is inaccessible
    };
};

—end note]

(Retained verbatim in C++03)

> this example (without int g(I* p) function) compiles both with and without -std=c++11 option

Yes, it does.
However, (AFAIK) it should not have compiled cleanly (with -std=c++98 -pedantic-errors.)

EDIT:
should not have compiled cleanly unless there was an accepted defect-report in the period 2003-2011

Annex C.2 "This subclause lists the differences between C ++ and ISO C ++ 2003" does not list this. However, Annex C is informative, not normative.
Last edited on
Now I got the picture, thanks.
Topic archived. No new replies allowed.