Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C++0x - nested initializer lists?

42 views
Skip to first unread message

er

unread,
Jun 22, 2011, 1:15:26 PM6/22/11
to
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 }
};
}

Victor Bazarov

unread,
Jun 23, 2011, 9:55:56 AM6/23/11
to

What's "s_"?

V
--
I do not respond to top-posted replies, please don't ask

er

unread,
Jun 23, 2011, 11:03:17 AM6/23/11
to
> What's "s_"?
>
> V
> --
> I do not respond to top-posted replies, please don't ask

Sorry,

typedef std::string s_;

Saeed Amrollahi

unread,
Jun 25, 2011, 4:25:03 AM6/25/11
to

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

Saeed Amrollahi

unread,
Jun 25, 2011, 10:01:50 AM6/25/11
to
On Jun 22, 8:15 pm, er <er.ci.2...@gmail.com> wrote:


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

0 new messages