Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Aggregate initializers in function arguments

Reply
Thread Tools

Aggregate initializers in function arguments

 
 
Unforgiven
Guest
Posts: n/a
 
      04-25-2005
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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-25-2005
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
 
Reply With Quote
 
 
 
 
Unforgiven
Guest
Posts: n/a
 
      04-25-2005
"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:uncbe.65986$(E-Mail Removed)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


 
Reply With Quote
 
Unforgiven
Guest
Posts: n/a
 
      04-25-2005
"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:uncbe.65986$(E-Mail Removed)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


 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      04-25-2005
Unforgiven wrote:
> "Victor Bazarov" <(E-Mail Removed)> wrote in message
> news:uncbe.65986$(E-Mail Removed)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?
 
Reply With Quote
 
Samee Zahur
Guest
Posts: n/a
 
      04-26-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Call again a variadic function (... variable number of arguments)with same arguments that its variadic wrapper moreau.steve@gmail.com C Programming 3 12-31-2008 07:13 AM
how to pass a function name and its arguments inside the arguments of other function? jmborr Python 1 11-03-2007 08:20 AM
ASP Aggregate Function Aaron ASP General 3 12-15-2006 06:11 PM
function default arguments from other arguments tutmann C++ 4 10-17-2006 08:00 PM
function call with arguments which takes no arguments Neo C Programming 10 01-20-2005 06:31 AM



Advertisments