Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tuple vector and initializer_list

I tried to compile the following snippets with gcc4.7

vector<pair<int,char> > vp = {{1,'a'},{2,'b'}}; //For pair vector, it works like a charm.  vector<tuple<int,double,char> > vt = {{1,0.1,'a'},{2,4.2,'b'}}; 

However, for the vector of tuples, the compiler complains:

error: converting to ‘std::tuple’ from initializer list would use explicit constructor ‘constexpr std::tuple< >::tuple(_UElements&& ...) [with _UElements = {int, double, char}; = void; _Elements = {int, double, char}]’

The error info spilled by the compiler is total gibberish for me, and I have no idea how were the constructors of tuple implemented, yet I do know they're totally okay with uniform initialization (like: tuple<int,float,char>{1,2.2,'X'}), therefore, I wonder if the problem I encountered is only a TODO of the compiler or it's something defined by the C++11 standard.

like image 431
Need4Steed Avatar asked Sep 15 '12 10:09

Need4Steed


People also ask

What is a tuple vector?

A tuple is an object that can hold a number of elements and a vector containing multiple number of such tuple is called a vector of tuple. The elements can be of different data types. The elements of tuples are initialized as arguments in order in which they will be accessed.

How do you initialize STD tuple?

Initializing a std::tuple We can initialize a std::tuple by passing elements as arguments in constructor i.e. std::tuple<int, double, std::string> result1 { 22, 19.28, "text" }; // Creating and Initializing a tuple std::tuple<int, double, std::string> result1 { 22, 19.28, "text" };


2 Answers

The relevant std::tuple constructors are explicit. This means that what you want to do is not possible, since the syntax you want to use is defined in terms of copy initialization (which forbids calling an explicit constructor). In contrast, std::tuple<int, float, char> { 1, 2.2, 'X' } uses direct initialization. std::pair does have non-explicit constructors only.

Either use direct-initialization or one of the Standard tuple factory function (e.g. std::make_tuple).

like image 182
Luc Danton Avatar answered Oct 02 '22 15:10

Luc Danton


This is actually doable, with c++11 features.

Yes the initializer_list wants all its element to be of the same type. The trick is that we can create a wrapper class that can be static_cast to all the types we want. This is easy to achieve:

 template <typename... tlist>  class MultiTypeWrapper {  };   template <typename H>  class MultiTypeWrapper<H> {  public:    MultiTypeWrapper() {}     MultiTypeWrapper(const H &value) : value_(value) {}     operator H () const {      return value_;    }  private:    H value_;  };   template <typename H, typename... T>  class MultiTypeWrapper<H, T...>     : public MultiTypeWrapper<T...> {   public:    MultiTypeWrapper() {}     MultiTypeWrapper(const H &value) : value_(value) {}     // If the current constructor does not match the type, pass to its ancestor.    template <typename C>    MultiTypeWrapper(const C &value) : MultiTypeWrapper<T...>(value) {}     operator H () const {      return value_;    }  private:    H value_;  }; 

With the implicit conversion constructors, we can pass something like {1,2.5,'c',4} to an initializer_list (or vector, which implicitly converts the initializer_list) of type MultiTypeWrapper. This means that we can not write a function like below to accept such intializer_list as argument:

template <typename... T> std::tuple<T...> create_tuple(std::vector<unit_test::MultiTypeWrapper<T...> > init) {   .... } 

We use another trick to cast each value in the vector to its original type (note that we provide implicit conversion in the definition of MultiTypeWrapper) and assign it to the corresponding slot in a tuple. It's like a recursion on template arguments:

template <int ind, typename... T> class helper { public:   static void set_tuple(std::tuple<T...> &t, const std::vector<MultiTypeWrapper<T...> >& v) {     std::get<ind>(t) = static_cast<typename std::tuple_element<ind,std::tuple<T...> >::type>(v[ind]);     helper<(ind-1),T...>::set_tuple(t,v);   } };    template <typename... T> class helper<0, T...> { public:   static void set_tuple(std::tuple<T...> &t, const std::vector<MultiTypeWrapper<T...> >& v) {     std::get<0>(t) = static_cast<typename std::tuple_element<0,std::tuple<T...> >::type>(v[0]);   } };    template <typename... T> std::tuple<T...> create_tuple(std::vector<unit_test::MultiTypeWrapper<T...> > init) {   std::tuple<T...> res;   helper<sizeof...(T)-1, T...>::set_tuple(res, init);   return res; } 

Note that we have to create the helper class for set_tuple since c++ does not support function specialization. Now if we want to test the code:

auto t = create_tuple<int,double,std::string>({1,2.5,std::string("ABC")}); printf("%d %.2lf %s\n", std::get<0>(t), std::get<1>(t), std::get<2>(t).c_str()); 

The output would be:

1 2.50 ABC 

This is tested on my desktop with clang 3.2

Hope my input helps :)

like image 33
BreakDS Avatar answered Oct 02 '22 15:10

BreakDS