Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #79085 > unrolled thread
| Started by | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| First post | 2021-04-19 08:16 -0700 |
| Last post | 2021-04-21 15:45 -0700 |
| Articles | 11 — 5 participants |
Back to article view | Back to comp.lang.c++
Injected class name Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-19 08:16 -0700
Re: Injected class name Bo Persson <bo@bo-persson.se> - 2021-04-19 20:05 +0200
Re: Injected class name Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-19 14:21 -0700
Re: Injected class name Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-20 18:06 -0700
Re: Injected class name Christian Hanné <the.hanne@gmail.com> - 2021-04-21 03:35 +0200
Re: Injected class name Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-21 06:29 -0700
Re: Injected class name Richard Damon <Richard@Damon-Family.org> - 2021-04-21 09:54 -0400
Re: Injected class name Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-21 07:35 -0700
Re: Injected class name Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-21 07:48 -0700
Re: Injected class name Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-19 11:34 -0700
Re: Injected class name "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-21 15:45 -0700
| From | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| Date | 2021-04-19 08:16 -0700 |
| Subject | Injected class name |
| Message-ID | <s5k6tc$cm$1@dont-email.me> |
Hello
A simple example
class A
{
};
class B : A
{
A *p; // 1 - OK
};
class C : B
{
A *p; // 2 - Error
};
The declaration at point 2 is invalid, since it refers to injected class
name `A` in `A`. That name is inaccessible in `C`.
But what about point 1? Does it also refer to injected class name `A` in
`A`? If so, it shouldn't compile either, since innards of `A` are
inaccessible in `B`.
Yet, it compiles. Apparently, when `A` is a direct base, unqualified
name lookup finds a different access path to `A` (not the injected class
name).
What exactly does it find? Where is it described in the standard? Is it
something as simple as "base class list is also searched during
unqualified name lookup"?
--
Best regards,
Andrey Tarasevich
[toc] | [next] | [standalone]
| From | Bo Persson <bo@bo-persson.se> |
|---|---|
| Date | 2021-04-19 20:05 +0200 |
| Message-ID | <ie5v3oF8lk9U1@mid.individual.net> |
| In reply to | #79085 |
On 2021-04-19 at 17:16, Andrey Tarasevich wrote:
> Hello
>
> A simple example
>
> class A
> {
> };
>
> class B : A
> {
> A *p; // 1 - OK
> };
>
> class C : B
> {
> A *p; // 2 - Error
> };
>
> The declaration at point 2 is invalid, since it refers to injected class
> name `A` in `A`. That name is inaccessible in `C`.
>
> But what about point 1? Does it also refer to injected class name `A` in
> `A`? If so, it shouldn't compile either, since innards of `A` are
> inaccessible in `B`.
>
> Yet, it compiles. Apparently, when `A` is a direct base, unqualified
> name lookup finds a different access path to `A` (not the injected class
> name).
>
> What exactly does it find? Where is it described in the standard? Is it
> something as simple as "base class list is also searched during
> unqualified name lookup"?
>
Yes. :-)
My compiler says:
'A' not accessible because 'B' uses 'private' to inherit from 'A'
So in B the base class is visible, and accessible as a private part.
In C it is visible, but not accessible - as it is a private member of B.
You can change that by making the inheritance public:
class B : public A
{
};
Or you can change C to refer to the global name A, instead of to the
private base.
class C : B
{
::A *p;
};
[toc] | [prev] | [next] | [standalone]
| From | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| Date | 2021-04-19 14:21 -0700 |
| Message-ID | <s5ks9i$7mp$1@dont-email.me> |
| In reply to | #79093 |
On 4/19/2021 11:05 AM, Bo Persson wrote:
>
> Yes. :-)
In both cases the injected name is referenced. In the first case it is
accessible, in the second - not, in accordance with normal everyday
access rules.
I just had a brainfart and somehow saw a problem where there was none.
> Or you can change C to refer to the global name A, instead of to the
> private base.
>
> class C : B
> {
> ::A *p;
> };
>
... or one I use an elaborated type specifier
class C : B
{
class A *p;
};
which apparently simply ignores access restrictions.
--
Best regards,
Andrey Tarasevich
[toc] | [prev] | [next] | [standalone]
| From | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| Date | 2021-04-20 18:06 -0700 |
| Message-ID | <s5ntrb$nt0$1@dont-email.me> |
| In reply to | #79099 |
On 4/19/2021 2:21 PM, Andrey Tarasevich wrote:
>
> ... or one can use an elaborated type specifier
>
> class C : B
> {
> class A *p;
> };
>
> which apparently simply ignores access restrictions.
>
Scratch that. This appears to be a bug in GCC. Neither Clang nor MSVC
allow that.
--
Best regards,
Andrey Tarasevich
[toc] | [prev] | [next] | [standalone]
| From | Christian Hanné <the.hanne@gmail.com> |
|---|---|
| Date | 2021-04-21 03:35 +0200 |
| Message-ID | <s5nvhq$kjf$1@gioia.aioe.org> |
| In reply to | #79130 |
>> ... or one can use an elaborated type specifier
>> class C : B
>> {
>> class A *p;
>> };
>> which apparently simply ignores access restrictions.
> Scratch that. This appears to be a bug in GCC. Neither Clang nor MSVC
> allow that.
No, that's perfectly legal code !
[toc] | [prev] | [next] | [standalone]
| From | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| Date | 2021-04-21 06:29 -0700 |
| Message-ID | <s5p9bm$1ab$1@dont-email.me> |
| In reply to | #79131 |
On 4/20/2021 6:35 PM, Christian Hanné wrote:
>>> ... or one can use an elaborated type specifier
>>> class C : B
>>> {
>>> class A *p;
>>> };
>>> which apparently simply ignores access restrictions.
>
>> Scratch that. This appears to be a bug in GCC. Neither Clang nor MSVC
>> allow that.
>
> No, that's perfectly legal code !
Sorry, but a mere "that's perfectly legal code" will not fly here with me.
If you claim that a private name `A` should be accessible here through
elaborated type specifier `class A` (and that Clang and MSVC are wrong
in rejecting this code), you need to explain in detail what makes you
think so. You need to quote the C++ standard.
--
Best regards,
Andrey Tarasevich
[toc] | [prev] | [next] | [standalone]
| From | Richard Damon <Richard@Damon-Family.org> |
|---|---|
| Date | 2021-04-21 09:54 -0400 |
| Message-ID | <O8WfI.15065$1%.7010@fx37.iad> |
| In reply to | #79137 |
On 4/21/21 9:29 AM, Andrey Tarasevich wrote:
> On 4/20/2021 6:35 PM, Christian Hanné wrote:
>>>> ... or one can use an elaborated type specifier
>>>> class C : B
>>>> {
>>>> class A *p;
>>>> };
>>>> which apparently simply ignores access restrictions.
>>
>>> Scratch that. This appears to be a bug in GCC. Neither Clang nor MSVC
>>> allow that.
>>
>> No, that's perfectly legal code !
>
> Sorry, but a mere "that's perfectly legal code" will not fly here with me.
>
> If you claim that a private name `A` should be accessible here through
> elaborated type specifier `class A` (and that Clang and MSVC are wrong
> in rejecting this code), you need to explain in detail what makes you
> think so. You need to quote the C++ standard.
>
I would need to dig hard through the standard to figure out which is
right, but the name A does get into class C by two different paths. One
is that it is a global name, the other is that it is a private base of a
base class.
A basic rule is accessibility does not affect visibility, so if the
visibility via base class has priority of the global visibility, it will
hide it without scoping, and then be not accessible.
Apparently there is a difference in how it was read by two compiler
writers, (or at least implemented) and a careful study would be needed
to see which is right.
[toc] | [prev] | [next] | [standalone]
| From | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| Date | 2021-04-21 07:35 -0700 |
| Message-ID | <s5pd8b$uqq$1@dont-email.me> |
| In reply to | #79138 |
On 4/21/2021 6:54 AM, Richard Damon wrote:
> On 4/21/21 9:29 AM, Andrey Tarasevich wrote:
>> On 4/20/2021 6:35 PM, Christian Hanné wrote:
>>>>> ... or one can use an elaborated type specifier
>>>>> class C : B
>>>>> {
>>>>> class A *p;
>>>>> };
>>>>> which apparently simply ignores access restrictions.
>>>
>>>> Scratch that. This appears to be a bug in GCC. Neither Clang nor MSVC
>>>> allow that.
>>>
>>> No, that's perfectly legal code !
>>
>> Sorry, but a mere "that's perfectly legal code" will not fly here
with me.
>>
>> If you claim that a private name `A` should be accessible here through
>> elaborated type specifier `class A` (and that Clang and MSVC are wrong
>> in rejecting this code), you need to explain in detail what makes you
>> think so. You need to quote the C++ standard.
>>
>
> I would need to dig hard through the standard to figure out which is
> right, but the name A does get into class C by two different paths. One
> is that it is a global name, the other is that it is a private base of a
> base class.
>
> A basic rule is accessibility does not affect visibility, so if the
> visibility via base class has priority of the global visibility, it will
> hide it without scoping, and then be not accessible.
>
> Apparently there is a difference in how it was read by two compiler
> writers, (or at least implemented) and a careful study would be needed
> to see which is right.
Simple experiments show that no, there isn't.
Firstly, GCC will issue a proper error message if one uses a plain type
name `A` instead of elaborated type specifier `class A`. So, this is not
just about name `A`, it is specifically about how an *elaborated type
specifier* is treated. This is much more specific, supposedly easier to
research, and I don't see anything in the standard that would provide
special treatment to this situation.
Secondly, here's a slightly different experiment
class A
{
struct X {};
};
class B : A
{
class X x;
};
This example also compiles fine in GCC.
Note that this time there's no "open" access path to `A::X` from
*anywhere*. Regardless of which side you approach it from, `A::X` is
private, private and private. It is hopelessly inaccessible, no matter
how you slice it.
Yet GCC manages to ignore access protection in this example. (Again, for
elaborated type specifier only.)
This is almost certainly a bag-o-feature of GCC. It could have been done
deliberately (although `-pedantic` doesn't help), but there seems to be
nothing in the standard about this kind of special treatment.
--
Best regards,
Andrey Tarasevich
[toc] | [prev] | [next] | [standalone]
| From | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| Date | 2021-04-21 07:48 -0700 |
| Message-ID | <s5pe0c$3kg$1@dont-email.me> |
| In reply to | #79139 |
On 4/21/2021 7:35 AM, Andrey Tarasevich wrote:
>
> Secondly, here's a slightly different experiment
>
> class A
> {
> struct X {};
> };
>
> class B : A
> {
> class X x;
> };
>
> This example also compiles fine in GCC.
The difference in class-key is unintentional. Use `class X` in all cases
for consistency.
> This is almost certainly a bag-o-feature of GCC.
I meant to say "bug-o-feature" :)
--
Best regards,
Andrey Tarasevich
[toc] | [prev] | [next] | [standalone]
| From | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| Date | 2021-04-19 11:34 -0700 |
| Message-ID | <s5kifr$uam$1@dont-email.me> |
| In reply to | #79085 |
On 4/19/2021 8:16 AM, Andrey Tarasevich wrote:
> Hello
>
> A simple example
>
> class A
> {
> };
>
> class B : A
> {
> A *p; // 1 - OK
> };
>
> class C : B
> {
> A *p; // 2 - Error
> };
>
> The declaration at point 2 is invalid, since it refers to injected class
> name `A` in `A`. That name is inaccessible in `C`.
>
> But what about point 1? Does it also refer to injected class name `A` in
> `A`? If so, it shouldn't compile either, since innards of `A` are
> inaccessible in `B`.
>
> Yet, it compiles. Apparently, when `A` is a direct base, unqualified
> name lookup finds a different access path to `A` (not the injected class
> name).
>
> What exactly does it find? Where is it described in the standard? Is it
> something as simple as "base class list is also searched during
> unqualified name lookup"?
>
Ugh... A stupid question with an obvious answer: the injected class name
is a _public_ member of its class. So the injected `A::A` is a public
member of `A`. It is naturally accessible in `B`. Nothing unusual or
remarkable there. The fact that `B` inherits from `A` privately does not
interfere with that at all.
--
Best regards,
Andrey Tarasevich
[toc] | [prev] | [next] | [standalone]
| From | "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> |
|---|---|
| Date | 2021-04-21 15:45 -0700 |
| Message-ID | <s5q9uc$59h$1@gioia.aioe.org> |
| In reply to | #79085 |
On 4/19/2021 8:16 AM, Andrey Tarasevich wrote:
> Hello
>
> A simple example
>
> class A
> {
> };
>
> class B : A
> {
> A *p; // 1 - OK
> };
>
> class C : B
> {
> A *p; // 2 - Error
> };
>
> The declaration at point 2 is invalid, since it refers to injected class
> name `A` in `A`. That name is inaccessible in `C`.
>
> But what about point 1? Does it also refer to injected class name `A` in
> `A`? If so, it shouldn't compile either, since innards of `A` are
> inaccessible in `B`.
>
> Yet, it compiles. Apparently, when `A` is a direct base, unqualified
> name lookup finds a different access path to `A` (not the injected class
> name).
>
> What exactly does it find? Where is it described in the standard? Is it
> something as simple as "base class list is also searched during
> unqualified name lookup"?
>
A long time ago, when I was first learning C++, I would do something like:
struct A
{
int m_A_foo;
};
struct B : A
{
int m_B_foo;
};
;^)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web