C++0x - nested initializer lists?

E

er

Hi,

This does not compile under gcc-4.4: converting to 'std::tuple<>' from
initializer list would use explicit constructor. Is it conformant
anyway? Thanks.

{
typedef std::tuple<s_, int> t_;
typedef std::vector<t_> v_;
v_ v = {
{ "a", 1 },
{ "b", 2 },
{ "c", 3 },
{ "d", 4 },
{ "e", 5 }
};
}
 
V

Victor Bazarov

This does not compile under gcc-4.4: converting to 'std::tuple<>' from
initializer list would use explicit constructor. Is it conformant
anyway? Thanks.

{
typedef std::tuple<s_, int> t_;
typedef std::vector<t_> v_;
v_ v = {
{ "a", 1 },
{ "b", 2 },
{ "c", 3 },
{ "d", 4 },
{ "e", 5 }
};
}

What's "s_"?

V
 
S

Saeed Amrollahi

Hi,

This does not compile under gcc-4.4: converting to 'std::tuple<>' from
initializer list would use explicit constructor. Is it conformant
anyway? Thanks.

    {
        typedef std::tuple<s_, int> t_;
        typedef std::vector<t_> v_;
        v_ v = {
            {  "a", 1  },
            {  "b", 2  },
            {  "c", 3  },
            {  "d", 4  },
            {  "e", 5  }
        };
    }

Hi

Sorry for late feedback. Your code doesn't compile under g++ 4.6.0
too,
and I don't know it is standard conformance or not,
but if you have tuple with two 2 arguments you can use pair, actually
the following code is compiled and run:

#include <string>
#include <utility>
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>

typedef std::string s_;
typedef std::pair<s_, int> p_;
typedef std::vector<p_> v_;

v_ v = { { "a", 1},
{ "b", 2}
};

int main()
{
using namespace std;
for (v_::size_type sz = 0; sz < v.size(); ++sz) {
cout << "{ " << v[sz].first << ", " << v[sz].second << "}" <<
'\n';
}
return 0;
}

I try to find the reason behind tuple and initializer list

HTH,
-- Saeed Amrollahi
 
S

Saeed Amrollahi

Hi,

This does not compile under gcc-4.4: converting to 'std::tuple<>' from
initializer list would use explicit constructor. Is it conformant
anyway? Thanks.

    {
        typedef std::tuple<s_, int> t_;
        typedef std::vector<t_> v_;
        v_ v = {
            {  "a", 1  },
            {  "b", 2  },
            {  "c", 3  },
            {  "d", 4  },
            {  "e", 5  }
        };
    }


I study the C++ standard document and asked your question
from C++ committee members.
You can't use initializer lists
with tuple because, tuple constructors are explicit, but the pair
constructors are not. You have to use the make_tuple function:

typedef std::vector<std::tuple<std::string, int>> v_;
v_ v = {
std::make_tuple("a", 1),
std::make_tuple("b", 2)
};

HTH,
-- Saeed Amrollahi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top