我开始了解boost :: variant.我认为这个例子应该有效.
#include <boost/fusion/sequence.hpp>
#include <boost/fusion/include/sequence.hpp>
#include <boost/variant/variant.hpp>
#include <string>
#include <vector>
#include <iostream>
#include <boost/variant/get.hpp>
boost::variant< bool,long,double,std::string,
std::vector<boost::variant<bool> > > v4;
void main()
{
std::vector<boost::variant<bool> > av (1);
v4= av;
try
{
bool b=
boost::get<bool> (v4[0]); // <--- this is line 20
std::cout << b;
}
catch (boost::bad_get v)
{
std::cout << "bad get" <<std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我收到编译错误:
d:\ m\upp\boosttest\main.cpp(20):错误C2676:二进制'[':'boost :: variant'不会定义此运算符或转换为预定义运算符可接受的类型[T0_ = bool,T1 = long,T2 = double,T3 = std :: string,T4 = std :: vector>]
Baf*_*ois 10
v4[0]因为v4是变体而不是向量,所以无效.您需要先使用它boost::get来检索存储在其中的向量.所以,第20行应该是
boost::get<bool>(boost::get<std::vector<boost::variant<bool> > >(v4)[0]);
| 归档时间: |
|
| 查看次数: |
5181 次 |
| 最近记录: |