Junior — Middle
What new features were introduced in C++ in the 2011 version?
sobes.tech AI
Answer from AI
In C++11, significant improvements and new features were introduced, including:
- Automatic type deduction (auto) — allows the compiler to determine the variable type automatically.
- Enhanced lambda expressions — anonymous functions with the ability to capture variables.
- Smart pointers (std::unique_ptr, std::shared_ptr) — for safe memory management.
- Constant expressions (constexpr) — compile-time computations.
- Range-based for loops — simplified syntax for iterating over collections.
- Move semantics and rvalue references — optimization for resource transfer.
- static_assert — compile-time condition checking.
- New data types (nullptr, enum class) — safe null pointer and type-safe enumerations.
- Multithreading (std::thread, std::mutex) — standard library support for threading.
Example of using auto and lambda:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4};
for (auto x : v) {
std::cout << x << " ";
}
std::cout << std::endl;
auto sum = [](int a, int b) { return a + b; };
std::cout << sum(3, 4) << std::endl;
return 0;
}