What is the difference

closed account (EwCjE3v7)
What is the difference
1
2
3
4
int units_sold = 0; 
int units_sold = {0}; 
int units_sold{0}; 
int units_sold(0);


Thank you
There is no difference.:)
Also I could append your list with the following examples

1
2
int units_sold = {}; 
int units_sold {}; 
closed account (EwCjE3v7)
There is
1
2
3
long double ld = 3.1415926536;
 int a{ld}, b = {ld}; // error: narrowing conversion required
 int c(ld), d = ld;   // ok: but value will be truncated 

The compiler rejects the initializations of a and b because using a long double to initialize an int is likely to lose data. At a minimum, the fractional part of ld will be truncated. In addition, the integer part in ld might be too large to fit in an int

But I don't get it
This is totally another case. It differs from the previous one.
Note that the third version (with the curly braces) is valid C++11 but not valid C++98/C++03

int units_sold{0};

error C2470: 'units_sold' : looks like a function definition, but there is no
parameter list; skipping apparent body


Same with vlad from moscow's second version.

And vlad from moscow's first version isn't valid C++98/C++03 either, for a different reason.

error C2552: 'units_sold' : non-aggregates cannot be initialized with
initializer list
1>        'int' is not an array or class : Types which are not array or class
types are not aggregate


C++11 has tidied up the initialization syntax so it's nice and consistent!

Andy
Last edited on
closed account (EwCjE3v7)
But what does this mean


long double ld = 3.1415926536;
int a{ld}, b = {ld}; // error: narrowing conversion required
int c(ld), d = ld; // ok: but value will be truncated

The compiler rejects the initializations of a and b because using a long double to initialize an int is likely to lose data. At a minimum, the fractional part of ld will be truncated. In addition, the integer part in ld might be too large to fit in an int

@CaptainBlastXD

But what does this mean


It means exactly that "The compiler rejects the initializations of a and b because using a long double to initialize an int is likely to lose data. At a minimum, the fractional part of ld will be truncated. In addition, the integer part in ld might be too large to fit in an int "

I think there is written clear enough.
closed account (EwCjE3v7)
But why are a and b bad but c and d just find?.like I know the fractional part will be taken away
Andywestken:

I am having trouble using the C++11 form of variable declaration and initialization on Visual Studio Express 2012. If I use the following code in VS Express, it fails but it works in DevC++:

int age{54};

I cannot figure out what I am doing wrong in VS Express. If I add the equal sign it works though...

I appreciate any advice you me provide...
It's just that Visual C++ is not yet fully compliant with C++11.

If the book you're working from uses a lot of C++11 specific code, you're going to be better off sticking to g++ while your working though the book.

Andy

PS While extended initializer lists aren't mentioned, you can see there are plenty of other "no"s in the C++11 feature list.

C++11 Features (Modern C++)
http://msdn.microsoft.com/en-us/library/vstudio/hh567368.aspx

Last edited on
Thanks Andy. I am working through the Stephen Prata book "C++ Primer Plus" - 6th edition. I will use the DevC++ as my primary learning tool but I also intend to "try" out my coding use Visual C++ too just to stay familiar withe the working of that IDE .

Thanks again...

Anthony
This article might be of interest.

Brace, brace!
http://akrzemi1.wordpress.com/2011/06/29/brace-brace/

(Found here: Brace Initialization syntax
http://www.cplusplus.com/forum/general/53756/ )

Andy
But what does this mean

long double ld = 3.1415926536;
int b = {ld}; // error: narrowing conversion required
int d = ld; // ok: but value will be truncated

The latter syntax has been in the standard for quite a while and invokes implicit conversion. A decent compiler can warn you about such usage. They could have changed it, but there is always the legacy code to consider (although C++11 has a new auto too).

The former syntax is new. It cannot break old code, so it has the liberty to be different. More strict.
Last edited on
Topic archived. No new replies allowed.