从boost :: fusion :: tuple访问第0个元素

时间:2013-04-28 02:31:04

标签: c++ boost tuples boost-fusion

通常我会以下列方式访问常规元组元素(比如说0)

mytuple->get<0>();

但是如果元组是boost :: fusion :: tuple类型,我如何访问第0个元素

更多详情

我有类似的东西

typedef boost::fusion::tuple<double,double,double,std::string,double,double,int, 
                            double,double,double,double,int,  
                            double,double,double,double,double,
                            double,double,double,double,double,
                            double,double,double,double> tuple_def; 

typedef boost::shared_ptr<tuple_def> my_tuple_def;

现在我正在使用它如下

shared_tuple_def btuple = boost::make_shared<tuple_def>(boost::fusion::make_tuple(323,0,0,"A",0,0,0,
                                                                                  0,0,0,0,0,
                                                                                  0,0,0,0,0,
                                                                                  0,0,0,0,0,
                                                                                  0,0,0,0));

如何访问第323个元素?

2 个答案:

答案 0 :(得分:5)

您可以使用boost::fusion::getboost::fusion::at

#define FUSION_MAX_VECTOR_SIZE 26
#include <string>
#include <iostream>
#include <boost/fusion/tuple.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/fusion/include/at.hpp>
#include <boost/mpl/int.hpp>
typedef boost::fusion::tuple<double,double,double,std::string,double,double,int, 
                            double,double,double,double,int,  
                            double,double,double,double,double,
                            double,double,double,double,double,
                            double,double,double,double> tuple_def; 

typedef boost::shared_ptr<tuple_def> shared_tuple_def;

int main() {
    shared_tuple_def btuple =
        boost::make_shared<tuple_def>(
            boost::fusion::make_tuple(
                323,0,0,"A",0,0,0,
                0,0,0,0,0,
                0,0,0,0,0,
                0,0,0,0,0,
                0,0,0,0));

    std::cout << boost::fusion::get<0>(*btuple) << "\n";
    std::cout << boost::fusion::at<boost::mpl::int_<0> >(*btuple) << "\n";
}

答案 1 :(得分:3)

您可以使用boost::fusion::get

int main()
{
    shared_tuple_def btuple = boost::make_shared<tuple_def>
    (
        boost::fusion::make_tuple
        (
            323, 0, 0, "A", 0, 0, 0,
            0, 0, 0, 0, 0,
            0, 0, 0, 0, 0,
            0, 0, 0, 0, 0,
            0, 0, 0, 0
        )
    );
    std::cout << boost::fusion::get<0>(*btuple) << std::endl;
}