简体   繁体   English

为什么这个boost :: variant示例不起作用?

[英]Why is this boost::variant example not working?

I am getting to know boost::variant. 我开始了解boost :: variant。 I think this example should work. 我认为这个例子应该有效。

#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; 
    }
}

I get a compilation error: 我收到编译错误:

d:\\m\\upp\\boosttest\\main.cpp(20) : error C2676: binary '[' : 'boost::variant' do es not define this operator or a conversion to a type acceptable to the predefined operator with [ T0_=bool, T1=long, T2=double, T3=std::string, T4=std::vector> ] d:\\ m \\ upp \\ boosttest \\ main.cpp(20):错误C2676:二进制'[':'boost :: variant'不会定义此运算符或转换为预定义运算符可接受的类型[T0_ = bool,T1 = long,T2 = double,T3 = std :: string,T4 = std :: vector>]

v4[0] is not valid since v4 is a variant, not a vector. v4[0]无效,因为v4是变体,而不是矢量。 You need to use boost::get to retrieve the vector stored in it first. 您需要首先使用boost::get来检索存储在其中的向量。 So, line 20 should be 所以,第20行应该是

boost::get<bool>(boost::get<std::vector<boost::variant<bool> > >(v4)[0]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM