Voi*_*oid 5 c++ boost-variant c++11
有没有办法让这项工作?我希望你能得到这个想法,我试图通过递归对创建一个列表
#include <boost/variant.hpp>
#include <utility>
struct nil {};
typedef boost::make_recursive_variant<nil, std::pair<int, boost::recursive_variant_ >>::type list_t;
int main() {
list_t list = { 1, (list_t){ 2, (list_t){ 3, nil() } } };
return 0;
}
Run Code Online (Sandbox Code Playgroud)
不。 boost::variant 的要点是它具有固定大小,并且不进行动态分配。这样就类似于工会了。递归 boost::variant 必须具有无限大小才能包含其最大可能值 - 显然是不可能的。
但是,您可以通过指针传递它来完成此操作。例如:
struct nil { };
typedef boost::make_recursive_variant<nil,
std::pair<int, boost::scoped_ptr<boost::recursive_variant_> > >
variant_list_int;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1393 次 |
| 最近记录: |