views:

161

answers:

2

I tried the following code:

int main()
{
   int x {23.22};
}

which includes an initialization that requires narrowing, but the code compiles fine without any error or warning. On the other hand, the following code gives error:

int main()
{
   int x[]{23.22};
}

Have I found a bug or what?

PS: I'm currently using GCC 4.5.0

+7  A: 

Looks like a bug. The following is straight out from the draft n3092:

8.5.4 List-initialization

— Otherwise, if the initializer list has a single element, the object is initialized from that element; if a narrowing conversion (see below) is required to convert the element to T, the program is ill-formed.

int x1 {2}; // OK
int x2 {2.0}; // error: narrowing

You can take a look at GCC's C++0X compliance here. The status of Initializer Lists (N2672) is 'Yes' -- but note that this is merely experimental (and hence you can expect bugs).

Update from bug report: GCC does emit a warning with the -Wconversion flag (and no this is not covered by -Wall).

dirkgently
I know about the feature, but it also mentions that if there is a possibility of narrowing to occur, the initialization should fail, doesn't it?
Saurabh Manchanda
The table lists implemented features, but not which features are implemented well. Version 4.4.1 seems to be struggling quite badly with the new initialization syntax and initializer lists, not sure if it is better with 4.5
UncleBens
@UncleBens: These are experimental builds; I wouldn't expect top quality from these builds.
dirkgently
@dirkgently: So, shouldn't GNU's page tell if the features have been only partially implemented?
Saurabh Manchanda
@OP: Did you file a bug already? :-) Here's what I found: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45378 (for the rest)
dirkgently
_Since this standard is still being extended and modified, the feature set provided by the experimental C++0x mode may vary greatly from one GCC version to another._ That's more than enough to keep you off from complaining ;-)
dirkgently
Yes, after posting here, I did file one. If it's not a bug, someone from their end would come up, I hope!
Saurabh Manchanda
@wacky_coder: I've taken the liberty to edit the title to be a bit more descriptive (and we can retrieve later on faster ...)
dirkgently
A: 

As C++0x support is still being implemented, even if there should be an error or warning according to the Standard and there isn't, that doesn't make it a bug necessarily, just yet to be implemented. This may also occur if the draft Standard has been changed since that particular feature was implemented.

The fact of working with work-in-progress software or standards is that things which are supposed to exist according to the latest spec don't.

DeadMG