2008-10-30

C++0x and the final features!

C++0x is now feature complete! My OSNews feeds have notified me that the C++0x specification has been finalized! What does this mean for us? Well here's a quick run down of C++0x changes from Wikipedia
  1. Extern Templates: using the extern keyword in front of template to prevent a template from being initialized in that translation unit.
  2. Initializer Lists: a new structure std::initializer_list<T> which can be used in a similar manner to C style struct initialization. This allows for syntax like: vector<int> v = { 1, 3, 5 };
  3. Range Based For: Say you have an array, and want to iterate through it all. A new usage of the for loop allows for this. for( int& x: myArray ) will iterate through the entire range of myArray and refer x to the current range element.
  4. Lambda Functions: This gets pretty cool! I'm not exactly sure how to explain it without a code sample, so here it is: std::for_each(someList.begin(), someList.end(), [&total](int x) {total += x}); What it basically does is create an anonymous ( excuse the Java lingo ) function where within the [] you can set the variable where the result of the function is to be stored. It just allows for exactly this sort of thing to be done, it's a great accompaniment to the STL algorithms. These lambda functions behave like friends to the class in which they are declared, so you have access to the member variables and functions.
  5. Constructor improvements: Ever had many constructors and needed to add a member variable to the class? You're liable to make mistakes and skip initialization lists for every var, and end up with an unitialized variable somewhere. Of course you could have a seperate init function, but thats obviously less efficient than using initilization lists ( since default constructors must be called anyway ). So, how about being able to call a constructor from another: SomeType(int newNumber) : number(newNumber) {} SomeType() : SomeType(42) {} Awesome!
  6. A Standard NULL: We've all been using NULL for a while. Now it's time to get used to nullptr. The great thing about nullptr is that it will work with overloading. Take for example two methods: doSomething( int bob ); doSomething( char* bob ); if i call doSomething( NULL ), what will get called? We'd hope the char* version of it gets called, but thats wrong, since NULL == 0, so the int version actually gets called. nullptr changes this.
  7. UTF-8,16 and 32 support for chars: char32_t* myUTFString = U"This is a UTF-32 string."
And many more changes, consult the Wiki page for more detail. Am glad to see that the C++ language is improving and trying to keep up with the times. Languages like Java and C# are growing greatly in popularity and preventing programmers from having full control over their applications performance. Keep up the good work to all the people keeping the standards alive!

No comments:

Post a Comment