C++0x - nested initializer lists?

Discussion in 'C++' started by er, Jun 22, 2011.

  1. er

    er Guest

    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 }
    };
    }
    er, Jun 22, 2011
    #1
    1. Advertising

  2. On 6/22/2011 1:15 PM, er wrote:
    > 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
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 23, 2011
    #2
    1. Advertising

  3. er

    er Guest

    > What's "s_"?
    >
    > V
    > --
    > I do not respond to top-posted replies, please don't ask


    Sorry,

    typedef std::string s_;
    er, Jun 23, 2011
    #3
  4. On Jun 22, 8:15 pm, er <> wrote:
    > 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
    Saeed Amrollahi, Jun 25, 2011
    #4
  5. On Jun 22, 8:15 pm, er <> wrote:
    > 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
    Saeed Amrollahi, Jun 25, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pmb
    Replies:
    2
    Views:
    373
  2. Replies:
    4
    Views:
    480
    Thorsten Ottosen
    Oct 14, 2004
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    384
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. Richard
    Replies:
    8
    Views:
    339
    Ian Collins
    Mar 2, 2007
  5. Kenshin
    Replies:
    0
    Views:
    282
    Kenshin
    Dec 9, 2009
Loading...

Share This Page