Comprehensive Overview of C++11 Language Features
Extended Integer Types
C++11 introduces long long and unsigned long long types for 64-bit integer support. Implementation sizes vary by platform:
- Visual Studio:
int(4 bytes),long(4 bytes),long long(8 bytes) - Linux:
int(4 bytes),long(8 bytes),long long(8 bytes)
New character types char16_t and char32_t provide support for 16-bit and 32-bit characters.
Uniform Initialization
Braced initialization extends to all built-in and user-defined types:
int value = {10};
double data{3.14};
short numbers[5]{1, 2, 3, 4, 5};
int* array = new int[4]{2, 4, 6, 8};
Class initialization with braces:
struct Person {
int id;
std::string name;
Person(int i, std::string n) : id(i), name(n) {}
};
Person p1(1, "Alice"); // Traditional syntax
Person p2 = {2, "Bob"}; // C++11 brace syntax
Person p3{3, "Charlie"}; // C++11 brace syntax
Containers support initializer_list constructors:
#include <initializer_list>
double calculateSum(std::initializer_list<double> values) {
double total = 0.0;
for (auto it = values.begin(); it != values.end(); ++it)
total += *it;
return total;
}
double result = calculateSum({1.1, 2.2, 3.3});
Null Pointer Constant
The nullptr keyword provides type-safe null pointer representation:
int* ptr = nullptr; // Type-safe null pointer
// int* bad = 0; // Potentially ambiguous
// int* worse = NULL; // Legacy macro
Strongly Typed Enumerations
Enum classes provide scoped and typed enumerations:
enum class Color { Red, Green, Blue };
enum class Status : uint8_t { Ok, Error, Warning };
Color c = Color::Red;
// Color d = Red; // Error: must scope
Class Member Initialization
In-class member initialization simplifies constructor definitions:
class Configuration {
int timeout = 30;
std::string host = "localhost";
char mode = 'A';
Configuration(int t, std::string h) : timeout(t), host(h) {}
};
Type Inference
auto and decltype enable type inference:
auto value = 42; // int
auto name = "text"; // const char*
auto list = {1, 2, 3}; // std::initializer_list<int>
decltype(value) another = value; // Same type as value
Range-based For Loops
Simplified iteration over containers:
std::vector<int> numbers = {1, 2, 3, 4, 5};
for (auto& num : numbers) {
num *= 2;
}
New STL Containers
C++11 adds several container types:
array: Fixed-size array with STL interfaceforward_list: Singly-linked list- Unordered containers:
unordered_map,unordered_multimap,unordered_set,unordered_multiset
Default and Deleted Functions
Control compiler-generated functions:
class NonCopyable {
int data = 0;
std::string text = "default";
public:
NonCopyable() = default;
NonCopyable(int d, std::string t) : data(d), text(t) {}
NonCopyable(const NonCopyable&) = delete;
void display() { /* ... */ }
};
Lambda Expressions
Anonymous function objects with capture semantics:
std::vector<int> values = {5, 8, 3, 1, 9};
std::sort(values.begin(), values.end(),
[](int a, int b) { return a > b; });
int threshold = 5;
auto filter = [threshold](int x) { return x > threshold; };
Capture options:
[=]: Capture by value[&]: Capture by reference[var]: Capture specific variable[this]: Capture class instance
Rvalue References and Move Semantics
Efficient resource transfer through move operations:
class ResourceHolder {
int* data = nullptr;
public:
ResourceHolder() = default;
// Move constructor
ResourceHolder(ResourceHolder&& other) noexcept {
data = other.data;
other.data = nullptr;
}
// Move assignment
ResourceHolder& operator=(ResourceHolder&& other) noexcept {
if (this != &other) {
delete data;
data = other.data;
other.data = nullptr;
}
return *this;
}
~ResourceHolder() { delete data; }
};
Perfect Forwarding
Preserve value categories in template functions:
template<typename T>
void forwardValue(T&& arg) {
process(std::forward<T>(arg));
}
Variadic Templates
Support for functions with variable arguments:
template<typename... Args>
void logMessage(const std::string& format, Args... args) {
// Process variable arguments
}
Multithreading Support
Standardized threading API:
#include <thread>
#include <mutex>
#include <condition_variable>
std::mutex dataMutex;
std::condition_variable dataCondition;
void workerThread(int id) {
std::unique_lock<std::mutex> lock(dataMutex);
dataCondition.wait(lock);
// Process data
}
std::thread t1(workerThread, 1);
std::thread t2(workerThread, 2);
Atomic Operations
Thread-safe atomic operations:
#include <atomic>
std::atomic<int> counter(0);
counter.fetch_add(1, std::memory_order_relaxed);
Smart Pointers
Automatic memory management:
#include <memory>
std::unique_ptr<Resource> res1 = std::make_unique<Resource>();
std::shared_ptr<Resource> res2 = std::make_shared<Resource>();
std::weak_ptr<Resource> weakRes = res2;
Function Wrappers and Bind
Uniform callable object handling:
#include <functional>
std::function<void(int)> callback;
callback = [](int x) { /* process x */ };
auto bound = std::bind(&Class::method, instance, std::placeholders::_1);
Type Traits and Static Assertions
Compile-time type checking:
#include <type_traits>
template<typename T>
void process(T value) {
static_assert(std::is_arithmetic<T>::value, "Numeric type required");
// Implementation
}
constexpr Functions
Compile-time evaluation:
constexpr int factorial(int n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}
constexpr int result = factorial(5); // Evaluated at compile-time
Delegating and Inheriting Constructors
Constructor reuse patterns:
class Base {
int x, y;
public:
Base(int a) : x(a), y(0) {}
Base(int a, int b) : x(a), y(b) {}
};
class Derived : public Base {
using Base::Base; // Inherit constructors
int z = 0;
};
Chrono Library
Standardized time handling:
#include <chrono>
auto start = std::chrono::high_resolution_clock::now();
// Code to measure
auto end = std::chrono::high_resolution_clock::now();
auto duration = end - start;
Numeric Conversions
Type-safe string conversions:
#include <string>
int value = std::stoi("42");
double num = std::stod("3.14159");
std::string text = std::to_string(123);