Implementing a class of a nameless namespace outside the namespaceblock

J

Juha Nieminen

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; }

class ::A { <implementation> };

Or does "::A" mean something else?
That compiles, but is it doing what I want? Note that this seems
to compile too:

namespace { class A; }

class A { <implementation> };

I don't really understand what is the meaning of that.
 
F

Fei Liu

Juha said:
This is possible:

namespace X { class A; }

class X::A { <implementation> };

However, what about nameless namespaces? Does this do what I want?
It's not nameless, it's anonymous (its name is hidden from you). What's
the logical outcome of this classification on the terminology then?
 
J

Juha Nieminen

It's not nameless, it's anonymous (its name is hidden from you). What's
the logical outcome of this classification on the terminology then?

A "yes" or "no" answer would have been more helpful.
 
M

Michael Oswald

Juha said:
namespace { class A; }

class A { <implementation> };


For what I understand, an anonymous namespace is something like that:

namespace { class A;}

translates to something like:

namespace some_unique_name_generated_from_compiler { class A; }
using some_unique_name_generated_from_compiler;


Otherwise you wouldn't have access to an anonymous namespace. So I think
your second solution is quite right.


lg,
Michael
 
E

Eric Johnson

A "yes" or "no" answer would have been more helpful.

Yes, there is something known as "unnamed" namespaces. If you have
access to Bjarne Stroustrup's "C++ Programming Language," he does a
good job of explaining it. The basic idea of unnamed namespaces is to
keep names within the unnamed namespace local to the translation unit
(i.e. source file) in which they were declared. What is unique about
unnamed namespaces is that when you use an unnamed namespace, there is
an implied using-directive. That way you can use the names that were
declared within the namespace, inside the same translation unit.

You also asked about this syntax:
class ::A { <implementation> };

My understanding is that whenever you proceed a name with "::", that
refers to the global namespace, which would be different from the
unnamed namespace. Do an experiment. Try creating a class 'A' in the
global namespace and one in an unnamed namespace. Give them different
public members or methods, so you can easily tell them apart and see
how you can use them in the same source file.

Good luck!
 
F

Fei Liu

Juha said:
A "yes" or "no" answer would have been more helpful.

What meaning does it hold for me to tell you 'yes' or 'no'. You would
blindly take someone else's answer over the internet? If you so disire,
then my answer is 'no'. I guess that's more helpful for you now...
 
J

Juha Nieminen

Fei said:
What meaning does it hold for me to tell you 'yes' or 'no'. You would
blindly take someone else's answer over the internet? If you so disire,
then my answer is 'no'. I guess that's more helpful for you now...

Then is it possible to do what I want? If yes, how?

It would seem odd to me that you can pre-declare a class in a
namespace and then declare/implement it outside of the namespace
block (with a "class TheNameSpace::TheClass { <implementation> };"
kind of syntax) but you couldn't do the same with an anonymous
namespace.

One could justly ask why not just implement it inside the namespace
block, as it's just visible inside that compilation unit (ie. that
source file) anyways. It's just a question of indentation (I would
prefer being able to implement the class outside the namespace block
to avoid an extra indentation level).

Of course one could then ask why not use a named namespace (and
a 'using' to effectively give the same effect as the anonymous
namespace). Well, that feels like a slightly worse solution because
a named namespace is not local to the current compilation unit like
an unnamed one is.
 
G

Greg Herlihy

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; }

class ::A { <implementation> };

Or does "::A" mean something else?
That compiles, but is it doing what I want? Note that this seems
to compile too:

No, ::A is in the global scope, the other A is not - they do not refer
to the same "A" class.
namespace { class A; }

class A { <implementation> };

Same as above.
I don't really understand what is the meaning of that.

Since an anoymous namespace has no name, there is no way to define any
member of anonymous namespace outside of the namespace, since there is
no name that can be used to qualify the definition (and that would at
the same time distinguish it from a definition in the current scope).
So any effort along these lines just ends up with a declaration in the
current scope and another one (with the same name) inside the
anonymous namespace.

Greg
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top