I have a namespace, N0
, that has sub-namespaces including N1
. The calling code only knows about the outer namespace. I'd like to write a function in the outer namespace that returns a std::unique_ptr<N1::T>
where that result is consumed elsewhere in N0
. However, the caller shouldn't know about N1
. What I'd like to do is something like:
// N0.h
namespace N0 {
typename T; // This isn't real C++.
std::unique_ptr<T> foo();
void bar(std::unique_ptr<T>&&);
}
// N0.cpp
#include "N1.h" // Get N1::T
namespace N0 {
typedef N1::T T;
...
}
That is, I'd like to expose a type that the caller can't see but internally I'd like to actually use a type in a different namespace. This way elsewhere someone could just forward-declare namespace N0 { class T; }
without having to know that T
is actually in N1
.
I could move T
itself into N0
, but it really belongs in N1
.
I could wrap T
with a dummy class in N0
, but that's ugly, and the pointer should basically do that.
I could probably make a class N0::T
that subclasses N1::T
, but that seems icky too.
Is there no way for N0
to forward declare that "I have a type and you don't need to know what it is" and have that type actually be in a different namespace? Put another way: Why is class C; class C{};
legal but class C; typedef int C;
is illegal? (Likewise class C; using C = int;
or typedef C; typedef int C;
.) They seem fundamentally the same to me and I can't think of a clever template trick to get around it. The only difference I can think of is that the typedef version wouldn't be subject to Koenig lookup.