By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,127 Members | 1,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,127 IT Pros & Developers. It's quick & easy.

Implementing a class of a nameless namespace outside the namespaceblock

P: n/a
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.
Mar 20 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Juha Nieminen wrote:
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?
>
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.
Mar 20 '07 #2

P: n/a
Fei Liu wrote:
>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?
A "yes" or "no" answer would have been more helpful.
Mar 20 '07 #3

P: n/a
Juha Nieminen wrote:
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
Mar 20 '07 #4

P: n/a
On Mar 20, 7:42 am, Juha Nieminen <nos...@thanks.invalidwrote:
Fei Liu wrote:
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?

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!

Mar 20 '07 #5

P: n/a
Juha Nieminen wrote:
Fei Liu wrote:
>>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?

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...
Mar 20 '07 #6

P: n/a
Fei Liu wrote:
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.
Mar 21 '07 #7

P: n/a
On Mar 20, 6:40 am, Juha Nieminen <nos...@thanks.invalidwrote:
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

Mar 21 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.