Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Overview: C++ Gets an Overhaul : Page 2

In this overview of the changes proposed for the new standard, you'll get an idea of the kinds of improvements you can expect this time around.


Convenience Features
Some C++0x features are meant to simplify recurring programming tasks and minimize boilerplate code. Most of these "convenience features" were borrowed from other programming languages. These convenience features include:

Listing 1demonstrates these features.

C++0x also removes some embarrassments from the language. For example, C++03 has at least three different forms of initialization:

int x=0;
int x(0);
int y[2]={1,2};

C++0x defines a unified initialization notation which can even be used in the declaration of dynamically allocated arrays:

int* a = new int[4] {1, 10, 20,95 };
vector<string> vs={"ab","cd","ef"};

class S {
 int arr[3];
  S() : arr{ 1, 2, 3 } {}

More Control in Your Hands
Certain new core features are meant to provide a standardized and uniform mechanism for controlling the compilation and execution environment programmatically. For example, take memory alignment. In C++03, you have to resort to compiler-dependent, non-portable hacks if you want to override the default alignment of your data. C++0x introduces two new operators: alignof and alignasthat query and override the alignment requirements of your data, respectively:

alignas (16) class Session
 long long timestamp;
 int authorizations;
 bool active;
cout<< alignof(Session) <<endl;

The C++98 rules regarding constant expressions force implementers to use low-tech macros instead of inline functions to ensure compile-time evaluation of constant expressions. This issue, and other limitations of C++98 constant expression rules, have led to the formation of a generalized constant expression mechanism in C++0x. Unlike const, the new constexprkeyword guarantees that the expression it qualifies can be used as a constant expression for example, in an array declaration:

const int y=func(); //dynamic initialization
int buff[y]; //error, y isn't a compile-time constant

constexpr int f(int x){ 
 return x * x; } //mandatory compile-time evaluation 
int arr[f(3)]; //OK, array's size is 9

Generally speaking, constexpr tells the programmer that a certain expression or object is evaluated and initialized at compile-time, as opposed to constwhich merely states that the object in question shall not change its state after initialization.

The onerous problem of undesirable implicit conversions that occur with C++98 conversion operators has also been addressed in C++0x. Up until now, programmers and library writers have had to rely on hacks such as the indirect conversion idiom to reduce the risk of undesirable conversions. In C++0x, you can declare conversion operators as explicit, thus gaining tighter control over the type of conversions that are permitted.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date