Aggregate initializers in function arguments

U

Unforgiven

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.
 
V

Victor Bazarov

Unforgiven said:
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
 
S

Samee Zahur

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top