Question

I'm breaking up a circular include dependency by forward declaring the class in its respective header, however that puts a small wrinkle in the existing convention of using a type alias inside of a class:

#include <memory>

class C {
 public:
  using Ptr = std::shared_ptr<C>;
};

Ideally it'd be possible to do something like:

#include <memory>

class C;
using C::Ptr = std::shared_ptr<C>;

But that's not possible because C isn't a complete type (yet). I realize it's possible to create an alias using CPtr = std::shared_ptr<C>;, but I was hoping I was missing something obvious using typename or some other keyword that would establish C as a complete-enough type for the purpose of creating a nested type alias.

Était-ce utile?

La solution

No, you cannot put something inside of a class except by actually writing it inside the class definition.

It's not an issue of whether C is a complete type or not. It's simply that C++ does not have any syntax that allows a name to be added to a class scope (or any scope) other than by actually writing it in that scope.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top