views:

206

answers:

3

I can forward declare a function in a namespace by doing this:

void myNamespace::doThing();

which is equivalent to:

namespace myNamespace
{
  void doThing();
}

To forward declare a class in a namespace:

namespace myNamespace
{
  class myClass;
}

Is there a shorter way to do this? I was thinking something along the lines of:

class myNamespace::myClass;
+2  A: 

I don't think so.

Tobias
and so don't I.
Michael Krelin - hacker
+8  A: 

I've wanted to do the same thing before - it's not allowed. A namespace member must be declared in a namespace-body. They can only be "referred to" using the scope resolution operator.

See 3.3.5 "Namespace scope" in the standard.

Entities declared in a namespace-body are said to be members of the namespace, and names introduced by these declarations into the declarative region of the namespace are said to be member names of the namespace.

and

A namespace member can also be referred to after the :: scope resolution operator (5.1) applied to the name of its namespace or the name of a namespace which nominates the member’s namespace in a using-directive;

Michael Burr
+11  A: 

No, however with a little reformatting

namespace myNamespace { class myClass; }

isn't much worse than

class myNamespace::myClass;
morechilli