473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Aggregate initializers in function arguments

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
Jul 23 '05 #1
5 2013
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
Jul 23 '05 #2
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:un******************@newsread1.mlpsca01.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
Jul 23 '05 #3
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:un******************@newsread1.mlpsca01.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
Jul 23 '05 #4
Unforgiven wrote:
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:un******************@newsread1.mlpsca01.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?
Jul 23 '05 #5
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

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kiel | last post by:
===== My error is: error C2512: no appropriate default constructor available I'm trying to use the default args for class B when it is an aggregate of class A. I could solve this problem with...
4
by: bobkaku | last post by:
I don't understand how I can fetch the result for an aggregate SQL function. I have a query written like this: $query = "select max(qno) from answers"; $result = mysql_query($query); $maxqno...
1
by: sausage31 | last post by:
I have a table as follows.... Device LotID Result1 Result2 Result3 aaa 1 5 10 15 bbb 1 2 4 6 aaa 2 ...
10
by: neb | last post by:
Dear member of the forum, Ms access has built-in aggregate function like: -Sum, Max, First, Avg, ... Is it possible to build user-defined aggregate? (if you have any clue, do not hesitate to...
2
by: Robert A Riedel | last post by:
This is a repost of a question that received no answer. In a module DATA.CPP, when attempting to initialize a __gc array as follows: // // Begin sample ... // // // Yes, I include all of...
1
by: Najib Abi Fadel | last post by:
Hi i have an ordered table of dates let's say: 1/1/2004 8/1/2004 15/1/2004 29/1/2004 5/2/2004 12/2/2004
3
by: Alden Pierre | last post by:
Hello, I'm having a hard time trying to figure why my code will not compile. When I try to compile the code, I get the non-aggregate type error. Any ideas on what I'm doing wrong? ...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
1
by: r035198x | last post by:
Inspiration Inspired by a post by Jos in the Java forum, I have put together a little article on class initializers. Initializers are indeed underutilized by most Java programmers. Once the Java...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.