这就是我想要做的:
boost::variant a<int, string>;
int b;
a=4;
b=a; //doesn't work. What is the easiest way to make b=4?
我知道我可以使用 get,但我希望能够在不指定类型的情况下执行此操作。我可以使用 apply_visitor 和访问者对象来实现,但我想知道是否有更简单的方法。
最佳答案
如果你有一个支持 C++0x 的编译器,你可以使用神奇的 decltype
:
boost::variant a<int, string>;
int b;
a = 4;
b = boost::get<decltype(b)>(a);
我不知道你为什么要这样做,因为你已经知道类型了。
关于c++ - boost::variant 获取最后访问的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7048201/