c++c++11usingforward-declaration

C++ Forward declare using directive


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?


Solution

  • 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>;
    }