Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

gcc, inheriting from a const-qualified superclass via a typedef

18 views
Skip to first unread message

Sam

unread,
Apr 4, 2019, 11:52:07 PM4/4/19
to
gcc 8.3.1 compiles the following without a single complaint:

=========================================================================

struct foo {

int hello_world=0;
};


typedef const foo c_foo;

struct bar : c_foo {
};

void foobar()
{
bar baz;

baz.hello_world=4;
}

=========================================================================

It seems likely there's a gcc bug hiding in here. The only other explanation
that lets gcc off the gook is that the standard allows inheritance from a
const-qualified base class, specifying that the const qualification gets
ignored. That seems somewhat unlikely to me. So, is the gcc bug here that:

1) "struct bar : c_foo" is ill-formed, can't inherit from a const-qualified
type.

2) "struct bar : c_foo" is not ill-formed, but gcc ignores that the parent
class is effectively const, and allows me to modify its members.

If inheriting from a const-qualified superclass is allowed, then there might
be another bug, because gcc doesn't like the following:

struct bar : const foo {
};

Öö Tiib

unread,
Apr 5, 2019, 3:53:32 AM4/5/19
to
On Friday, 5 April 2019 06:52:07 UTC+3, Sam wrote:
> gcc 8.3.1 compiles the following without a single complaint:
>
> =========================================================================
>
> struct foo {
>
> int hello_world=0;
> };
>
>
> typedef const foo c_foo;
>
> struct bar : c_foo {
> };
>
> void foobar()
> {
> bar baz;
>
> baz.hello_world=4;
> }
>
> =========================================================================
>
> It seems likely there's a gcc bug hiding in here. The only other explanation
> that lets gcc off the gook is that the standard allows inheritance from a
> const-qualified base class, specifying that the const qualification gets
> ignored. That seems somewhat unlikely to me.

Why? it is exactly so specified in [class.name]:
| A typedef-name that names a class type, or a cv-qualified version
| thereof, is also a class-name. If a typedef-name that names a
| cv-qualified class type is used where a class-name is required,
| the cv-qualifiers are ignored. A typedef-name shall not be used
| as the identifier in a class-head.

> So, is the gcc bug here that:
>
> 1) "struct bar : c_foo" is ill-formed, can't inherit from a const-qualified
> type.
>
> 2) "struct bar : c_foo" is not ill-formed, but gcc ignores that the parent
> class is effectively const, and allows me to modify its members.
>
> If inheriting from a const-qualified superclass is allowed, then there might
> be another bug, because gcc doesn't like the following:
>
> struct bar : const foo {
> };

That is ill-formed because it expects class-name there (or template
simple-template-id or decltype-specifier) with optional access-specifier.
It does not expect cv-qualified class type there and so can't parse it.

0 new messages