deriving from class in anonymous namespace

A

Al Grant

Consider two translation units, (1):

namespace { struct S { }; }
struct D: S { virtual int f(void); };

and (2):

#include <typeinfo>
struct D;
char const *f(D *p) { return typeid(p).name(); }

Does 'D' name the same type in (1) and (2)? The RTTI for
D* will reference an RTTI definition for D. Every ABI I've seen
will give D (and its RTTI) a well-defined name (e.g. _ZTI1D in
gABI), allowing external linkage to work here. But does the
standard guarantee that?

There's no ODR violation here, though a second definition of D
would presumably violate the ODR.

GCC 4.2 warns about deriving from a class in an anonymous
namespace, as if it was something to be ashamed of.
VC++ and Comeau don't warn.
 
N

Noah Roberts

Al said:
GCC 4.2 warns about deriving from a class in an anonymous
namespace, as if it was something to be ashamed of.
VC++ and Comeau don't warn.

Personally, I'd say GCC is correct to do so. What you are doing here is
publicly deriving from a private type. This makes no semantic sense.
Public inheritance is meant to be an "is-a" relationship and so what
you've done is claim D "is-a" private object, yet you're attempting to
expose it publicly.

You could improve the situation by using private inheritance. It is, it
seems to me, the only appropriate kind of inheritance for your situation.
 
J

James Kanze

Consider two translation units, (1):
namespace { struct S { }; }
struct D: S { virtual int f(void); };
#include <typeinfo>
struct D;
char const *f(D *p) { return typeid(p).name(); }
Does 'D' name the same type in (1) and (2)? The RTTI for D*
will reference an RTTI definition for D. Every ABI I've seen
will give D (and its RTTI) a well-defined name (e.g. _ZTI1D in
gABI), allowing external linkage to work here. But does the
standard guarantee that?
There's no ODR violation here, though a second definition of D
would presumably violate the ODR.
GCC 4.2 warns about deriving from a class in an anonymous
namespace, as if it was something to be ashamed of. VC++ and
Comeau don't warn.

It's a warning, not an error. In practice, either the
definition of D is in a header, in which case, including it in
two different translation units is undefined behavior, or it's
in a source file, in which case, D could also be in the unnamed
namespace. There are exceptions, though; I've done this a few
times when `D' was actually SomeClass::Impl and I wanted to
share the Impl (friend, second derived class, etc.) with other
classes in the source file, without making it publicly available
in the header. So all of the implementation was actually in the
class in the unnamed namespace, and SomeClass::Impl derived from
it. It's a fairly exceptional case, but IMHO reasonable enough
that the compiler shouldn't warn about it.
 
J

Juha Nieminen

Noah said:
Personally, I'd say GCC is correct to do so. What you are doing here is
publicly deriving from a private type. This makes no semantic sense.
Public inheritance is meant to be an "is-a" relationship and so what
you've done is claim D "is-a" private object, yet you're attempting to
expose it publicly.

You could improve the situation by using private inheritance. It is, it
seems to me, the only appropriate kind of inheritance for your situation.

Why? The only way code outside that compilation unit can see the
derived class is as a forward-declared incomplete type. Thus all they
see is just a pointer to an incomplete type. They don't know anything
about it, and that's ok, because it is defined in terms of a private
base class definition.

It's not possible to bring the full declaration of that derived class
to other compilation units without also bringing the full declaration of
the base class as well.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top