Is there a shorter way to forward declare a class in a namespace?
-
21-09-2019 - |
Question
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;
Solution
No, however with a little reformatting
namespace myNamespace { class myClass; }
isn't much worse than
class myNamespace::myClass;
OTHER TIPS
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;
I don't think so.