I have a header which exposes a templated class and a typedef via using, something like:
namespace fancy { struct Bar { ... } template<typename T> class Foo { ... } using FooBar = Foo<Bar>; }
I would like to forward declare FooBar
to use it in a shared_ptr
in another header. I've tried
namespace fancy { using FooBar; }
like for a class or struct, but without luck. Is this possible, and if so, how?
You can't declare a using
alias without defining it. You can declare your class template without defining it, however, and use a duplicate using
alias:
namespace fancy { template <typename> class Foo; class Bar; using FooBar = Foo<Bar>; }
If your using declaration is too large (a lot of template parameters, which on their turn are also defined by a using statement), you could also add a dummy forward struct that has the using type as a dependent type:
namespace fancy { struct Bar { ... } template<typename T> class Foo { ... } using FooBar = Foo<Bar>; // Forward struct struct FooBarFwd { using type = FooBar; } }
Then in your place where you want to forward declare:
namespace fancy { class FooBarFwd; } // use your type as typename FooBarFwd::type baz(const typename FooBarFwd::type & myFooBar); // instead of // FooBar baz(const FooBar & myFooBar);
Some disadvantages of this approach are
typename
to disambiguate the dependent type.FooBar
with typename FooBarFw::type
)Therefore, I advise to apply this technique only when you are certain what you are doing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With