1

通常我会通过以下方式访问一个常规元组元素(比如 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));

如何访问第 0 个元素,即 323 ?

4

2 回答 2

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";
}
于 2013-04-28T02:55:11.540 回答
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;
}
于 2013-04-28T02:52:54.780 回答