Aggregate initializers in function arguments

Discussion in 'C++' started by Unforgiven, Apr 25, 2005.

  1. Unforgiven

    Unforgiven Guest

    Me and some other people have been involved in a debate whether or not the
    following code is legal C++:
    #include <vector>

    struct s_stuff {
    std::string s_search;
    std::string s_url;
    };

    int main() {
    std::vector<s_stuff> v_items;
    v_items.push_back((s_stuff){"foo", "bar"}); // *
    return 0;
    }

    The important line is the one I marked with *. It is initializing a function
    argument with an aggregate initializer, as defined by the standard in 8.5.1.
    Now the standard says in section 8.5 that "The process of initialization
    described in the remainder of 8.5 applies also to initializations specified
    by other syntactic contexts, such as the initialization of function
    parameters
    with argument expressions (5.2.2) or the initialization of return values
    (6.6.3)." which would lead to be believe it is indeed possible to do this.

    However, both Comeau and Visual C++ 2003 (and 2005 beta 2 as well) claim
    that this is not valid C++. I even have a statement from the Visual C++ team
    that "This is not valid C++ - an initializer-clause that is enclosed in
    braces can only be part of the definition of a variable."

    The strange thing is, g++ compiles it fine.

    So, who's correct here? And if Comeau and VC are correct, where in the
    standard does it say so.

    Thanks in advance.

    --
    Unforgiven
    Unforgiven, Apr 25, 2005
    #1
    1. Advertising

  2. Unforgiven wrote:
    > Me and some other people have been involved in a debate whether or not the
    > following code is legal C++:
    > #include <vector>
    >
    > struct s_stuff {
    > std::string s_search;
    > std::string s_url;
    > };
    >
    > int main() {
    > std::vector<s_stuff> v_items;
    > v_items.push_back((s_stuff){"foo", "bar"}); // *


    It's not.

    > return 0;
    > }
    >
    > The important line is the one I marked with *. It is initializing a function
    > argument with an aggregate initializer, as defined by the standard in 8.5.1.
    > Now the standard says in section 8.5 that "The process of initialization


    The process, not the syntax.

    > described in the remainder of 8.5 applies also to initializations specified
    > by other syntactic contexts, such as the initialization of function
    > parameters
    > with argument expressions (5.2.2) or the initialization of return values
    > (6.6.3)." which would lead to be believe it is indeed possible to do this.


    Nope.

    >
    > However, both Comeau and Visual C++ 2003 (and 2005 beta 2 as well) claim
    > that this is not valid C++. I even have a statement from the Visual C++ team
    > that "This is not valid C++ - an initializer-clause that is enclosed in
    > braces can only be part of the definition of a variable."
    >
    > The strange thing is, g++ compiles it fine.


    Have you tried it in "strict" or "ANSI" mode?

    > So, who's correct here? And if Comeau and VC are correct, where in the
    > standard does it say so.


    See Annex A and search for "initializer-clause".

    V
    Victor Bazarov, Apr 25, 2005
    #2
    1. Advertising

  3. Unforgiven

    Unforgiven Guest

    "Victor Bazarov" <> wrote in message
    news:uncbe.65986$01.us.to.verio.net...
    > Unforgiven wrote:
    >> Me and some other people have been involved in a debate whether or not
    >> the following code is legal C++:
    >> #include <vector>
    >>
    >> struct s_stuff {
    >> std::string s_search;
    >> std::string s_url;
    >> };
    >>
    >> int main() {
    >> std::vector<s_stuff> v_items;
    >> v_items.push_back((s_stuff){"foo", "bar"}); // *

    >
    > It's not.


    Thanks. I figured Comeau was right, but I couldn't find in it in the
    standard. I've found it now.

    Thanks again

    --
    Unforgiven
    Unforgiven, Apr 25, 2005
    #3
  4. Unforgiven

    Unforgiven Guest

    "Victor Bazarov" <> wrote in message
    news:uncbe.65986$01.us.to.verio.net...
    > Unforgiven wrote:
    >> Me and some other people have been involved in a debate whether or not
    >> the following code is legal C++:
    >> #include <vector>
    >>
    >> struct s_stuff {
    >> std::string s_search;
    >> std::string s_url;
    >> };
    >>
    >> int main() {
    >> std::vector<s_stuff> v_items;
    >> v_items.push_back((s_stuff){"foo", "bar"}); // *

    >
    > It's not.


    One more thing. Do you perhaps know what is the rationale behind the
    standard disallowing this?

    --
    Unforgiven
    Unforgiven, Apr 25, 2005
    #4
  5. Unforgiven

    red floyd Guest

    Unforgiven wrote:
    > "Victor Bazarov" <> wrote in message
    > news:uncbe.65986$01.us.to.verio.net...
    >
    >>Unforgiven wrote:
    >>
    >>>Me and some other people have been involved in a debate whether or not
    >>>the following code is legal C++:
    >>>#include <vector>
    >>>
    >>>struct s_stuff {
    >>> std::string s_search;
    >>> std::string s_url;
    >>>};
    >>>
    >>>int main() {
    >>> std::vector<s_stuff> v_items;
    >>> v_items.push_back((s_stuff){"foo", "bar"}); // *

    >>
    >>It's not.

    >
    >
    > One more thing. Do you perhaps know what is the rationale behind the
    > standard disallowing this?
    >


    Because C didn't either?
    red floyd, Apr 25, 2005
    #5
  6. Unforgiven

    Samee Zahur Guest

    true... but C didn't allow a lot of things C++ does - that IS what the
    ++ initially stood for, right? I'm also a bit curious about why this is
    not allowed, and would like someone to tell me more. I mean,
    fun(myclass(5,6)) are quite legal ... creates a temporary object before
    passing it on. Now what's the problem with doing the same thing to a
    POD struct??

    Samee
    Samee Zahur, Apr 26, 2005
    #6
    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. Neo
    Replies:
    10
    Views:
    644
    sushant
    Jan 20, 2005
  2. jmborr
    Replies:
    1
    Views:
    400
    Stargaming
    Nov 3, 2007
  3. Aaron

    ASP Aggregate Function

    Aaron, Dec 12, 2006, in forum: ASP General
    Replies:
    3
    Views:
    238
    Mark McGinty
    Dec 15, 2006
  4. Ike Naar

    Re: Passing aggregate directly to function

    Ike Naar, Jun 27, 2013, in forum: C Programming
    Replies:
    2
    Views:
    174
    Keith Thompson
    Jun 27, 2013
  5. James Kuyper

    Re: Passing aggregate directly to function

    James Kuyper, Jun 27, 2013, in forum: C Programming
    Replies:
    0
    Views:
    176
    James Kuyper
    Jun 27, 2013
Loading...

Share This Page