C++11, C++0x
C++11 (originally nicknamed “C++0x”) is a major revision of the C++ language that introduced modern features for safer, cleaner, and more efficient programming. It was approved by ISO on 12 August 2011, replacing C++03. The name follows the tradition of naming language versions by the year of the specification's publication.
C++11 includes a wide range of features: major new features like lambda support and "move semantics", usability improvements like type inference through the auto keyword, simplified looping over containers, and many improvements that will make templates easier to write and easier to use. This series on C++11 will cover all of these features and many more.
C++11 includes several additions to the core language and extends the C++ standard library, incorporating most of the C++ Technical Report 1 (TR1) libraries — with the exception of the library of mathematical special functions. C++11 was published as ISO/IEC 14882:2011 in September 2011 and is available for a fee. The working draft most similar to the published C++11 standard is N3337, dated 16 January 2012; it has only editorial corrections from the C++11 standard. Work is currently under way on the C++14 and C++17 standards.
Why do we use C++11?
C++11 modernized the language by:
• Reducing common programming errors
• Making code more expressive and readable
• Adding built-in support for concurrency
• Improving performance through better memory handling
When should you use C++11?
In practice: almost always (unless you’re stuck with an old compiler).
Use it when:
• Writing modern C++ applications
• Working with multithreading
• Managing memory safely
• Developing high-performance systems
Key Features
1. Auto Keyword (Type Inference)
auto x = 10; // compiler deduces int
2. Range-Based For Loop
for (auto& val : vec) {
// easier iteration
}
3. Smart Pointers
Introduced into standard C++ (inspired by Boost):
std::shared_ptr
std::unique_ptr
4. Lambda Expressions (Anonymous Functions)
auto add = [](int a, int b) { return a + b; };
5. Move Semantics & Rvalue References
Improves performance by avoiding unnecessary copies:
std::vector v = std::move(other);
6. Multithreading Support
Standardized threading:
std::thread
std::mutex
std::lock_guard
7. nullptr
Replaces NULL with a safer alternative:
int* p = nullptr;
8. Uniform Initialization
int x{10};
std::vector v{1,2,3};
9. constexpr
Allows compile-time computation:
constexpr int square(int x) { return x * x; }
10. Static Assertions
static_assert(sizeof(int) == 4, "Unexpected size");
Advantages
• Safer memory management (smart pointers)
• Better performance (move semantics)
• Built-in concurrency support
• Cleaner and more expressive syntax
• Reduced reliance on external libraries like Boost
Disadvantages
• Requires modern compiler support
• Some features (like move semantics) can be complex
• Legacy codebases may need refactoring
The directives of C++
The modifications for C++ involve both the core language and the standard library. In the development of every utility of the 2011 standard, the committee has applied some directives:
• Maintain stability and compatibility with C++98 and possibly with C;
• Prefer introduction of new features through the standard library, rather than extending the core language;
• Prefer changes that can evolve programming technique;
• Improve C++ to facilitate systems and library design, rather than to introduce new features useful only to specific applications;
• Increase type safety by providing safer alternatives to earlier unsafe techniques;
• Increase performance and the ability to work directly with hardware;
• Provide proper solutions for real-world problems;
• Implement “zero-overhead” principle (additional support required by some utilities must be used only if the utility is used);
• Make C++ easy to teach and to learn without removing any utility needed by expert programmers.
Attention to beginners is considered important, because they will always compose the majority of computer programmers, and because many beginners would not intend to extend their knowledge of C++, limiting themselves to operate in the aspects of the language in which they are specialized.