如何解决typedef中的循环链接?

时间:2016-10-19 21:55:49

标签: c++ reference typedef cyclic

我需要以某种方式解决这个问题:

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

我收到类似这样的错误

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

任何帮助都将受到赞赏。

修改

使用结构作为代理可以轻松完成:

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

但必须更方便地做到这一点。

1 个答案:

答案 0 :(得分:0)

你不能这样做,因为你要求的东西导致无限递归:

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

将扩展为:

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;