[cfe-dev] Beginnings of C++ access control (and question about standard)

Sebastian Redl sebastian.redl at getdesigned.at
Mon Apr 20 10:22:20 PDT 2009


Tim Northover wrote:
> On Saturday 18 Apr 2009 22:53:01 you wrote:
>   
>> Tim Northover wrote:
>>     
>>> +  // [class.access.base]:p5
>>> +  // A member m is accessible at the point R when named in class N if:
>>>       
>> Please observe the standard quotation style; there's an automated tool
>> that collects the quotes. The style is:
>>     
>
> I didn't realise there was an automated tool involved there, I'd assumed it 
> was more informal. So should what follows be a direct quotation from the 
> standard, or is paraphrasing preferred?
>   
If you paraphrase, it's not a quote. So if you make a quote header,
quote exactly.
I paraphrase unnecessarily wordy parts sometimes, but if you do that, be
sure to wrap that part in [], e.g.
//   The frooble has [congualized] members of type snook.
> And presumably I shouldn't split up the standard comments after that without 
> including the header in each subpart?
>   
Eh, I do that all the time. I don't think the tool extracts the actual
quote.
> Anyway, onto the main question I have for now. The standard 
> ([class.access.base]p5) says that a member m is "accessible at point R if 
> named in class N" if (amongst other options):
>
> "m as a member of N is protected, and R occurs in a member or friend of class 
> N, or in a member or friend of a class P derived from N, where m as a member 
> of P is public, private, or protected".
>
> I interpret that as saying the following is valid:
> class N {
> protected:
>     int m;
> };
>
> class Y : private N {};
>
> class P : public N {
>     class B : public Y {
>         int f(N x) {
>             return x.x;
>         }
>     };
> };
>
> but gcc disagrees:
> scope.cpp:1: error: ‘class N’ is inaccessible
> scope.cpp:10: error: within this context
> scope.cpp: In member function ‘int P::B::f(N)’:
> scope.cpp:11: error: ‘class N’ has no member named ‘x’
>
> Am I wrong in my interpretation of that particular paragraph; or is that 
> actually forbidden on other grounds (perhaps ambiguity resolution?); or is gcc 
> wrong?
>   
Comeau agrees with GCC, so I'd say they're right. This is because the N
that is found would be the N within N that Y inherited as private.

That is, in every class, there exists an alias of that class by the same
name. This alias is inherited and underlies normal access restrictions.
In other words, Y contains an N that is private, since it was inherited
from the private base N. B then inherits this N, but cannot access it.
The name lookup in P::B::f finds this N, instead of the N that P
inherited, because P::B is the inner scope and P the outer scope.

Indeed, if I change f(N x) to f(P::N x) (access the N that P inherited)
or f(::N x) (access the global, original N directly), Comeau accepts
this part. It still doesn't accept accessing x.m, which is protected and
you don't access it through P, much less x.x, which doesn't exist.

The naming class 'N' for the N in the signature of f is B, and the
member 'm' actually found is P::N, which is inaccessible in B.


By the way, please use Reply All to reply to cfe-dev messages. Your
message didn't go to the list.

Sebastian



More information about the cfe-dev mailing list