Array of initializer lists?

Discussion in 'C++' started by Juha Nieminen, Nov 18, 2011.

  1. I'm trying to do this:

    auto values[] = { { 1, 2, 3 }, { 4, 5, 6 } };

    but gcc 4.6 is giving me the error:

    error: unable to deduce 'std::initializer_list<auto>' from '{{1, 2, 3}, {4, 5, 6}}'

    If I make a small change, it doesn't help:

    auto values = { { 1, 2, 3 }, { 4, 5, 6 } };

    (the same error is issued.)

    Is this because the above code is invalid, or because gcc 4.6 doesn't
    yet work properly with such constructs?

    Note that this compiles and works as expected:

    auto values = { { 1, 2, 3 } };
    Juha Nieminen, Nov 18, 2011
    #1
    1. Advertising

  2. On 11/18/2011 11:18 AM, Juha Nieminen wrote:
    > I'm trying to do this:
    >
    > auto values[] = { { 1, 2, 3 }, { 4, 5, 6 } };
    >
    > but gcc 4.6 is giving me the error:
    >
    > error: unable to deduce 'std::initializer_list<auto>' from '{{1, 2, 3}, {4, 5, 6}}'
    >
    > If I make a small change, it doesn't help:
    >
    > auto values = { { 1, 2, 3 }, { 4, 5, 6 } };
    >
    > (the same error is issued.)
    >
    > Is this because the above code is invalid, or because gcc 4.6 doesn't
    > yet work properly with such constructs?
    >
    > Note that this compiles and works as expected:
    >
    > auto values = { { 1, 2, 3 } };


    The doubled-up braces are converted into single ones here, so no
    surprise that it's OK.

    When you supply a brace-enclosed initializer list, the compiler is
    supposed to initialize the object with 'initializer_list<blah>', and
    nested brace initializers probably call for
    'initializer_list<initializer_list<blah>>', which is likely too much for
    the compiler to deduce.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 18, 2011
    #2
    1. Advertising

  3. Victor Bazarov <> wrote:
    >> Note that this compiles and works as expected:
    >>
    >> auto values = { { 1, 2, 3 } };

    >
    > The doubled-up braces are converted into single ones here, so no
    > surprise that it's OK.


    That does not seem to be the case. There's a difference between
    "{ 1, 2, 3 }" and "{ { 1, 2, 3 } }".

    For example, this works:

    auto values = { 1, 2, 3 };
    for(auto& i: values)
    std::cout << i << std::endl;

    However, this does not:

    auto values = { { 1, 2, 3 } };
    for(auto& i: values)
    std::cout << i << std::endl;

    To make it work, it has to be done like:

    auto values = { { 1, 2, 3 } };
    for(auto& i: values)
    for(auto& j: i)
    std::cout << j << std::endl;

    which makes sense. 'values' is an array (or possibly an initializer list,
    I'm not completely sure) containing an initializer list, hence the need
    for the double loop.

    It would, thus, make sense if one could put more than one initializer
    list inside the array, like:

    auto values = { { 1, 2, 3 }, { 4, 5, 6 } };
    for(auto& i: values)
    for(auto& j: i)
    std::cout << j << std::endl;

    'values' would still be an array of initializer lists; it's just that
    in this case it contains two of them rather than one. However, as said,
    gcc 4.6 issues an error on the first line.
    Juha Nieminen, Nov 18, 2011
    #3
  4. Juha Nieminen wrote:

    > I'm trying to do this:
    >
    > auto values[] = { { 1, 2, 3 }, { 4, 5, 6 } };
    >
    > but gcc 4.6 is giving me the error:
    >
    > error: unable to deduce 'std::initializer_list<auto>' from '{{1, 2, 3},
    > {4, 5, 6}}'
    >
    > If I make a small change, it doesn't help:
    >
    > auto values = { { 1, 2, 3 }, { 4, 5, 6 } };
    >
    > (the same error is issued.)
    >
    > Is this because the above code is invalid, or because gcc 4.6 doesn't
    > yet work properly with such constructs?
    >
    > Note that this compiles and works as expected:
    >
    > auto values = { { 1, 2, 3 } };


    I think you want to read http://stackoverflow.com/q/7699963/34509 . Note
    that "auto x = { ... }" is no different from transforming "auto x = { ... }"
    to the following

    template<typename T> void trans(initializer_list<T>);
    trans({...});

    "initializer_list<T>", with the deduced T (as described in the answer on
    that stackoverflow question) will be the type of the auto variable "x".
    Johannes Schaub, Nov 19, 2011
    #4
    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:
    382
  2. Replies:
    4
    Views:
    489
    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:
    397
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. Richard
    Replies:
    8
    Views:
    350
    Ian Collins
    Mar 2, 2007
  5. Kenshin
    Replies:
    0
    Views:
    286
    Kenshin
    Dec 9, 2009
Loading...

Share This Page