Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Forward declare using directive

Tags:

c++

c++11

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?

like image 505
Mike M Avatar asked Nov 20 '13 22:11

Mike M


Video Answer


2 Answers

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>; } 
like image 174
Dietmar Kühl Avatar answered Sep 22 '22 19:09

Dietmar Kühl


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

  • Using typename to disambiguate the dependent type.
  • Extra indirection for your type, some compilers could have problems when reporting errors.
  • Changing to this approach might need quite a lot of changes to your code (changing every occurence of FooBar with typename FooBarFw::type)

Therefore, I advise to apply this technique only when you are certain what you are doing.

like image 24
Simon Marynissen Avatar answered Sep 20 '22 19:09

Simon Marynissen