c++typedefcyclic-reference

How to resolve cyclical links in typedefs?


I need to resolve this thing somehow:

  class MyType; // this thing doesn't help here
  typedef std::stack<boost::variant<int, std::function<shared_ptr<MyType>()>>> MyType;

I get an error like this one

 error C2371: 'MyType': redefinition; different basic types

Any help will be appreciate.

Edit:

This can be done easily with using structure as proxy:

struct MyStruct;
typedef std::stack<boost::variant<int, std::function<shared_ptr<MyStruct>()>>> MyType;
struct MyStruct {
    MyType data;
};

But must be more handy way to do this.


Solution

  • You can't do this, because what you are asking for causes an infinite recursion:

    typedef std::stack<boost::variant<int, std::function<shared_ptr<MyType>()>>> MyType;
    MyType a;
    

    would expand to:

    std::stack<boost::variant<int, std::function<shared_ptr<
        std::stack<boost::variant<int, std::function<shared_ptr<
            std::stack<boost::variant<int, std::function<shared_ptr<
                *Infinitely many more here*
            ()>>>>
        ()>>>>
    ()>>> a;