⬅ Previous Next ➡

C++11/14/17 Modern Features

C++11/14/17 Modern Features
  • Modern C++ (C++11/14/17) introduced features that make code safer, shorter, and faster.
  • Important modern features: auto, range-based loops, nullptr, lambda, move semantics, smart pointers, and constexpr.

1) auto Keyword

  • auto lets compiler deduce the variable type automatically.
  • Useful with complex STL types and iterators.
#include <iostream>
#include <vector>
using namespace std;

int main() {
    auto x = 10;       // int
    auto y = 2.5;      // double

    vector<int> v = {1, 2, 3};

    for (auto it = v.begin(); it != v.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;

    return 0;
}

2) Range-based for Loop

  • Easy loop for arrays and STL containers.
  • Syntax: for (type x : container)
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> v = {10, 20, 30};

    for (int x : v) {
        cout << x << " ";
    }
    cout << endl;

    return 0;
}

3) nullptr

  • nullptr is a type-safe null pointer constant (better than NULL/0).
#include <iostream>
using namespace std;

int main() {
    int *p = nullptr;

    if (p == nullptr) {
        cout << "Pointer is null" << endl;
    }
    return 0;
}

4) Lambda Expressions

  • A lambda is an anonymous function: [capture](params){ body }
  • Useful in STL algorithms (sort, for_each, etc.).
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    vector<int> v = {5, 1, 4, 2};

    sort(v.begin(), v.end(), [](int a, int b) {
        return a < b; // ascending
    });

    for_each(v.begin(), v.end(), [](int x) {
        cout << x << " ";
    });
    cout << endl;

    return 0;
}

5) Move Semantics (Basic Idea)

  • Move semantics transfers resources instead of copying (faster).
  • Used heavily by STL containers and smart pointers.
  • std::move converts an object to an rvalue reference to enable move.
#include <iostream>
#include <string>
#include <utility>
using namespace std;

int main() {
    string a = "Hello";
    string b = std::move(a); // moves data from a to b

    cout << "b = " << b << endl;
    cout << "a = " << a << endl; // a becomes empty or unspecified

    return 0;
}

6) Smart Pointers

  • Smart pointers automatically manage memory (no manual delete).
  • Located in <memory>.
  • Types:
    • unique_ptr: single owner
    • shared_ptr: shared ownership (ref count)
    • weak_ptr: non-owning reference to shared_ptr
#include <iostream>
#include <memory>
using namespace std;

int main() {
    unique_ptr<int> p = make_unique<int>(10);
    cout << *p << endl;

    shared_ptr<int> s1 = make_shared<int>(50);
    shared_ptr<int> s2 = s1;

    cout << "Use count: " << s1.use_count() << endl;
    return 0;
}

7) constexpr

  • constexpr means value/function can be evaluated at compile time.
  • Used for constants and fast calculations.
#include <iostream>
using namespace std;

constexpr int square(int x) {
    return x * x;
}

int main() {
    constexpr int a = 5;
    constexpr int ans = square(a);

    cout << "ans = " << ans << endl;
    return 0;
}

8) Quick Notes

  • auto reduces long type names.
  • range-for makes loops cleaner.
  • nullptr is safer than NULL.
  • lambda provides inline functions for algorithms.
  • move semantics improves performance by transferring ownership.
  • smart pointers prevent memory leaks.
  • constexpr enables compile-time constants.
⬅ Previous Next ➡