Monday, October 22, 2012

c++11 universal init vs tuples.

While messing around with tuples I noted a rather large headache with its array initialization. If im not mistaken the problem is caused by the choice to make the constructor explicit. Basically this will ultimately force the user to typedef the tuple and then explicitly construct each of the lines in the array init.


#include <iostream>
#include <tuple>

int array[2]{1,2};

struct TableEntryC
{
  int         a;
  double      b;
  const char* c;
};

TableEntryC tableC[2]
{
  {1,1.1,"test"},
  {2,2.2,"try"}
};

typedef std::tuple<int,double,std::string> TableEntry;

TableEntry entry{ 1, 1.1, "test" };

TableEntry table[2]
{
  TableEntry{1,1.1,"test"},
  TableEntry{2,2.2,"try" }
};

// bad bad bad...
//TableEntry bad_table[2]
//{
//  {1,1.1,"test"},
//  {2,2.2,"try" }
//};


int main()
{
  std::cout << std::get<1>(entry) << std::endl;
  std::cout << std::get<2>(table[1]) << std::endl;
}

No comments:

Post a Comment