c++11 std::array init

Discussion in 'C++' started by Chris Forone, May 16, 2013.

  1. Chris Forone

    Chris Forone Guest

    hello group,

    why can i initialize a local std::array in this way:

    std::array<float, 3> locvar{1.0f, 2.0f, 3.0f};

    but as member "only" in this way:

    std::array<float, 3> member{{1.0f, 2.0f, 3.0f}};

    i use mingw g++ 4.8.0 (32 bit).

    thanks a lot, chris
    Chris Forone, May 16, 2013
    #1
    1. Advertising

  2. Chris Forone

    Chris Forone Guest

    Am 16.05.2013 12:49, schrieb Sam:
    > Chris Forone writes:
    >
    >> hello group,
    >>
    >> why can i initialize a local std::array in this way:
    >>
    >> std::array<float, 3> locvar{1.0f, 2.0f, 3.0f};

    >
    > Because you can't. That's the short answer.
    >

    but i do and it compiles fine!

    >> but as member "only" in this way:
    >>
    >> std::array<float, 3> member{{1.0f, 2.0f, 3.0f}};

    >
    > These are two different things. The first one would be a constructor
    > with variadic parameters. The second one is a std::initializer_list. It
    > shouldn't be surpising that something called a "std::initializer_list"
    > would be used to initialize something.
    >
    > I suppose that there is no technical reason why a variadic list of
    > parameters cannot be used to initialize an array or a vector, but that's
    > just the way it is.
    >

    can a ctor have both kinds of braces? () {}
    im not surprised about the function of the initializer_list and dont
    understand it, if there is some kind of (sarcastic?) joke in there...
    Chris Forone, May 16, 2013
    #2
    1. Advertising

  3. Chris Forone

    Chris Forone Guest

    Am 16.05.2013 12:49, schrieb Sam:
    > Chris Forone writes:
    >
    >> hello group,
    >>
    >> why can i initialize a local std::array in this way:
    >>
    >> std::array<float, 3> locvar{1.0f, 2.0f, 3.0f};

    >
    > Because you can't. That's the short answer.
    >
    >> but as member "only" in this way:
    >>
    >> std::array<float, 3> member{{1.0f, 2.0f, 3.0f}};

    >
    > These are two different things. The first one would be a constructor
    > with variadic parameters. The second one is a std::initializer_list. It
    > shouldn't be surpising that something called a "std::initializer_list"
    > would be used to initialize something.
    >
    > I suppose that there is no technical reason why a variadic list of
    > parameters cannot be used to initialize an array or a vector, but that's
    > just the way it is.
    >


    ok, i think i understand now: the one is brace elision with copy-ctor,
    the other is direct aggregate-initialization of the c-style array in
    std::array<>

    cheers, chris
    Chris Forone, May 21, 2013
    #3
    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. Tony Cheng
    Replies:
    1
    Views:
    8,209
    Juan T. Llibre
    Feb 24, 2006
  2. Peter Jansson
    Replies:
    5
    Views:
    6,301
    Ivan Vecerina
    Mar 17, 2005
  3. Replies:
    1
    Views:
    657
    Jules
    Aug 18, 2005
  4. Jess
    Replies:
    4
    Views:
    441
  5. news.aon.at
    Replies:
    11
    Views:
    645
    Ian Collins
    Jan 29, 2011
Loading...

Share This Page