如何解决 typedef 中的循环链接?
How to resolve cyclical links in typedefs?
我需要以某种方式解决这个问题:
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;
};
但必须有更方便的方法来做到这一点。
你不能这样做,因为你要求的会导致无限递归:
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;
我需要以某种方式解决这个问题:
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;
};
但必须有更方便的方法来做到这一点。
你不能这样做,因为你要求的会导致无限递归:
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;