#1: User Defined Suffixes for Literals
C++ always allowed programmers to store and perform operations on integral types, floating-point types and strings. They were considered to be merely values with no consideration for what unit they represent. Now it is possible to use any user-defined unit that can increase readability or allow for mathematically correct operations.
double distance = 6.2_km + 3.6_km + 200_m + 216.4_m; cout << distance; // Can print 10.2164
Of course, this is not magic and C++ can’t do this by itself. It will require an operator overload for us to teach the compiler how to do this, but hey, it’s possible and that’s the point!
#2: A simpler way of looping using the range for loop
There is a far simpler way to iterate through containers and arrays. Instead of using:
for (vector<int>::iterator i=v.begin(); i!=v.end(); i++) { *i; }
We can now use this syntax to iterate through the container:
for (int i: v) { cout << i; }
We can use this syntax to iterate through the container and even make changes to the container elements:
for (int &i: v) { i *= 2; }
#3: Framing Constant Expressions That Can Even Involve Function Calls
Was this possible before:
int x[getRed()+getGreen()];
Obviously not! But it’s now possible albeit certain conditions apply.
#4: New Enumerations whose Enumerators Do Not Pollute Scope
Now C++ provides new enumerations whose enumerators are accessible only through the scope of the enumeration. This can eliminate clashes between like enumerators as shown below:
enum class Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat }; enum class SolarSystem { Sun, Earth, Moon };
#5: New NULLPointers that do not get converted to Integers
An annoying feature in C++ was that NULL pointers could well get converted to integers (they were actually implemented as integer 0). Now we have a new implementation of NULL pointer called nullptr that does not implicitly get converted to integers but can get converted automatically to any pointer type!
char *ptr = nullptr;
#6: More and better smart pointers!
The auto_ptr was a very useful addition to C++03 and allowed the programmer to be free from memory management worries. The only problem was that it used copy semantics to perform a move operation, which could be misleading to the uninitiated! C++11 adds unique_ptr as a replacement for auto_ptr, which works exactly the same way except that it specifically prohibits copy semantics and forces move semantics:
unique_ptr p1<int> (new int); unique_ptr p2<int>; p2 = p1; // Wrong: copy semantics p2 = std::move(p1); //Correct: move semantics
C++11 also adds shared_ptr to support sharing of memory ownership between smart pointers and weak_ptr to support copying of pointers from shared_ptr without owning memory.
#7: C++ can now automatically deduce data types!
Why should one type in complicated declarations when the compiler can deduce it! Check out the examples below:
auto x=10; // Assumed to be int auto y=x; // Assumed to be int auto d = getCurrentDate(); // Assumed to be Date (if that's what getCurrentDate() returns) for (auto x: v) { cout << x; } // Where v is a vector of any type that can be printed using cout decltype(y) z; // Assumed to be int
#8: Get more done quickly using Lambda Expressions!
Anonymous functions that can be typed in anywhere where a function pointer is required… neat?
auto add = [](int x, int y) -> int { return x+y; } cout << add(2,3); // Prints 5 cout << [](int x, int y) -> int { return x+y; }(2,3); // Prints 5
#9: Initialize Data Members Directly Inside a Class!
Have you been adding constructors to merely initialize data members with constant values? Directly initialize them now!
class Counter { int count=0; };
#10: Make Use of Initializer Lists as a convenient syntax to initialize objects
Here are some examples to show what is now possible:
Point p1 = {10,20}; Point p2{2,3}; return Point{x,y}; return {x,y};
#11: Generate parameterized derived class constructors that pass on the values to the base class constructor
Fed up of writing derived class parameterized constructors for formality? Let the compiler generate them for you!
class A { int x, y; public: A(int x, int y) : x(x), y(y){} }; class B: public A { public: using A::A; }; int main() { B b(6,2); return 0; }
#12: Use Tuples to store heterogenious data items
Found STL containers limiting because they are homogeneous? Here comes tuples to the rescue!
std::tuple<int, float> t(10,6.5f); int x = std::get<0>(t); std::get<0>(t) = 20;
Conclusion
These 12 enhancements by C++11 (and C++14) have been hand-picked but are not necessarily the most powerful or most important new features. There are many other enhancements, some being more fundamentally useful! Concepts like move semantics and move constructors, for example, have not been completely dealt here as they’d probably require more elaborate explanation. Have fun with C++11/C++14 nevertheless!