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