1

Normally I would access a regular tuple element (say 0) in the following way

mytuple->get<0>();

However if the tuple is of the type boost::fusion::tuple how do I access the 0th element

More Detail

I have something like this

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;

Now I am using it as follows

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));

How do I access the 0th element which is 323 ?

MistyD
  • 16,373
  • 40
  • 138
  • 240
  • Do I dare ask what tuple_def actually means? It's like a big pile of doubles and some other random pieces, seems like a good candidate to use a struct instead of a tuple. – John Zwinck Apr 28 '13 at 02:46
  • Initially it was only 5 elements until it increased to this much. I will eventually convert it to a struct. `tuple_def` is the fist type def. The first typedef is used in the next statement – MistyD Apr 28 '13 at 02:51
  • I guess that's why I avoid tuples almost entirely--it starts off with 5 elements and the next thing you know it's 25 and difficult to fix. :) – John Zwinck Apr 28 '13 at 02:55

2 Answers2

5

You can use boost::fusion::get or boost::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";
}
Mankarse
  • 39,818
  • 11
  • 97
  • 141
3

You could use 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;
}
awesoon
  • 32,469
  • 11
  • 74
  • 99