dimanche 29 novembre 2015

Iterate over tuple without dereferencing elements

I have a std::tuple (or a boost fusion tuple) whose elements cannot be trivially constructed (for example references) and I want to iterate over the types but not the values of the elements.

In this example I have a (general) tuple type and I want to generate a vector with (runtime) type information. The example below works if all the types in the sequence are trivially default constructed but not in general.

In summary, I want a function that transform: std::tuple<...> -> std::vector<std::type_index>

#include <boost/fusion/adapted/std_tuple.hpp>
#include <boost/fusion/algorithm/iteration/for_each.hpp>
#include <typeindex>
#include<vector>

using tuple_type = std::tuple<std::string&, int>;

int main(){
    std::vector<std::type_index> types;
    boost::fusion::for_each(
        tuple_type{},  // fails because of std::string&
        [&types](auto& e){types.push_back(typeid(e));} 
    ); 
}

The problem is that I have to generate runtime information from non runtime information and I can't figure out how to mix the fusion functions (http://ift.tt/1Op9aAy) and the metafunctions (http://ift.tt/1XCa6Cc).

I tried with boost::fusion::accumulate and boost::fold but the situation is always the same, at some point I have to generate a runtime element in order to apply the algorithm.

Aucun commentaire:

Enregistrer un commentaire