473,288 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

deriving from class in anonymous namespace

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.
Nov 7 '08 #1
3 3405
Al Grant wrote:
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.
Nov 7 '08 #2
On Nov 7, 4:52 pm, Al Grant <algr...@myrealbox.comwrote:
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.
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 7 '08 #3
Noah Roberts wrote:
Al Grant wrote:
>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.
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.
Nov 7 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: amine | last post by:
Hi, I have created a proxy class from a wsdl document and I want to add it to my project. but the generated proxy class contains packages like system.web that are not supported by the .NET...
3
by: Hans De Winter | last post by:
Hi, I am really puzzled why my compiler does not accept the following code:- --- CODE STARTS HERE --- #include <iostream> using namespace std; class Foo {
28
by: Steven T. Hatton | last post by:
This may be another question having an obvious answer, but I'm not seeing it. I'm trying to create a class that derives from std::valarray<std::string>. I don't need a template, and I haven't come...
4
by: marco_segurini | last post by:
Hi, the following test program shows a solution to a problem I have had. Now, this test program is compiled and linked by VS2003 and g++ while Comeau-on-line-compiler fails with this messages:...
3
by: Anonymous | last post by:
Hey, in my init I created some code to create a tabbed interface. The code worked very well. It simply adds a table with the required cells/rows to a placeholder, which is located on the page....
1
by: Jozsef Bekes | last post by:
Hi All, I need to derive a class in VB.Net from a COM interface declared in an idl file in a VC++ project. Here is the interface declaration:...
0
by: samiam | last post by:
Here are my requirements of the web service (.NET 1.1, XP/2003) (1) It needs to call a COM+ application that has already been written and deployed. The web service essentially becomes a client to...
1
by: David | last post by:
Help!!! // // Contents of ValueNode.cs // using System; using Microsoft.Web.UI.WebControls;
7
by: Juha Nieminen | last post by:
This is possible: namespace X { class A; } class X::A { <implementation}; However, what about nameless namespaces? Does this do what I want? namespace { class A; }
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.