C++11 and C++14 introduced key features such as move semantics, uniform initialization, lambda expressions, auto and decltype, concurrency and parallelism, generalized constant expressions, and binary literals and digit separators, improving performance, memory usage, and making it easier to write efficient, expressive, and type-safe code.

 

  1. Move Semantics - C++11 introduced a new type of reference, called an rvalue reference, which provides support for move semantics. This allows objects to be efficiently moved rather than copied, leading to improved performance and memory usage.
#include <iostream>
#include <utility>

int main() {
    std::string str = "Hello, World!";
    std::string str2 = std::move(str);
    std::cout << str2 << std::endl; // outputs "Hello, World!"
    return 0;
}

 

  • Uniform Initialization - C++11 introduced a new syntax for initializing objects, called uniform initialization. This provides a more flexible and consistent way of initializing objects and eliminates most forms of the "most vexing parse" problem. For example:
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = { 1, 2, 3, 4, 5 };
    for (const auto &e : v) {
        std::cout << e << " ";
    }
    std::cout << std::endl; // outputs "1 2 3 4 5 "
    return 0;
}
  • Lambda Expressions - C++11 introduced lambda expressions, which allow you to define anonymous functions (or closures) that can capture variables from the surrounding scope. This makes it easier to write code that uses functional concepts such as map, reduce, and filter. For example:
#include <iostream>
#include <algorithm>
#include <vector>

int main() {
    std::vector<int> v = { 1, 2, 3, 4, 5 };
    std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
    std::cout << std::endl; // outputs "1 2 3 4 5 "
    return 0;
}
  • Auto and Decltype - C++11 introduced the auto keyword and the decltype type-inference mechanism. This makes it easier to write type-safe code, especially when using complex types, and eliminates the need for explicitly specifying types in many cases. For example:
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = { 1, 2, 3, 4, 5 };
    for (const auto &e : v) {
        std::cout << e << " ";
    }
    std::cout << std::endl; // outputs "1 2 3 4 5 "
    return 0;
}
 

'C++' 카테고리의 다른 글

Compiling C++ Code on a Mac using the Terminal and GCC  (0) 2023.02.10
Key Features of C++14  (0) 2023.02.10
Lambda Functions in C++  (0) 2023.02.10
Function Pointers in C++  (0) 2023.02.10
Understanding Functions in C++  (0) 2023.01.12

+ Recent posts