gpt4 book ai didi

c++ - boost::variant 获取最后访问的类型

转载 作者:行者123 更新时间:2023-11-30 01:28:47 25 4
gpt4 key购买 nike

这就是我想要做的:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com