advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Partners & Affiliates
advertisement
advertisement
advertisement
Average Rating: 3.8/5 | Rate this item | 22 users have rated this item.
 Print Print
Overview: C++ Gets an Overhaul (cont'd)
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 1 demonstrates these features.
advertisement

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];
public:
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 alignas that 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 constexpr keyword 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 const which 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.

Previous Page: Introduction Next Page: Compatibility with Other Standards
advertisement
Page 1: IntroductionPage 3: Compatibility with Other Standards
Page 2: Convenience Features  
Please rate this item (5=best)
 1  2  3  4  5
DevX is a division of Internet.com.
© Copyright 2010 Internet.com. All Rights Reserved. Legal Notices
advertisement