Friday, May 28, 2010

C++0x - THE NEXT ISO C++ STANDARD

C++0x is the next ISO C++ standard . The previous version was referred as C++03 or C++98.
The official name of the committee forming this standard is SC22 WG21.
The new features implemented in this standard are :

1: __cplusplus
In C++0x the macro __cplusplus will be set to a value that differs from (is greater than) the current 199711L.

2: auto : deduction of a type from the initializer
Consider
auto u = 9 ;
here u will be of type int because that's the type of it's initializer.
In general
auto u = expression ;
the type of u is the type of the value computed from expression.




3: long long -- a longer integer

An integer that's at least 64 bits long. For example:

long long x = 9223372036854775807LL;
No, there are no long long longs nor can long be spelled short long long.

4: nullptr -- a null pointer literal
nullptr is a literal denoting the null pointer; it is not an integer:


5:Preventing narrowing
The problem:
C and C++ implicitly truncates:

int x = 7.3;  // Ouch!
void f(int);
f(7.3);   // Ouch!
However, in C++0x, {} initialization doesn't narrow:

int x1 = {7.3}; // error: narrowing
double d = 7;
int x2{d};  // error: narrowing (double to int)
char x3{7};  // ok: even though 7 is an int, this is not narrowing
vector vi = { 1, 2.3, 4, 5.6 }; // error: double to int narrowing
The way C++0x avoids a lot of incompatibilities is by relying on the actual values of initializers (such as 7 in the example above) when it can (and not just type) when deciding what is a narrowing conversion. If a value can be represented exactly as the target type, the conversion is not narrowing. Note that floating-point to integer conversions are always considered narrowing -- even 7.0 to 7.

No comments: