C++ Advanced concepts

1. Templates & Metaprogramming

๐Ÿ”น Template Specialization

Customize behavior for specific types.

template<typename T>
class Printer {
public:
void print(T value) { std::cout << value; }
};template<>
class Printer<bool> {
public:
void print(bool value) { std::cout << std::boolalpha << value; }
};

๐Ÿ”น Variadic Templates

Handle variable number of arguments.

template<typename... Args>
void printAll(Args... args) {
(std::cout << ... << args); // Fold expression (C++17)
}

๐Ÿ”น SFINAE & std::enable_if

Enable functions based on type traits.

template<typename T>
typename std::enable_if<std::is_integral<T>::value>::type
print(T t) { std::cout << "Integral\n"; }

๐Ÿ”น Concepts (C++20)

Cleaner constraints for templates.

template<typename T>
concept Integral = std::is_integral_v<T>;template<Integral T>
void func(T value) {}

2. Move Semantics & Perfect Forwarding

๐Ÿ”น Rvalue References (&&)

Enable move semantics.

class Test {
public:
Test(Test&& other) noexcept { /* move resources */ }
};

๐Ÿ”น std::move

Converts to rvalue reference.

std::vector<int> v1 = {1,2,3};
std::vector<int> v2 = std::move(v1);

๐Ÿ”น Perfect Forwarding

template<typename T>
void wrapper(T&& arg) {
process(std::forward<T>(arg));
}

3. Smart Pointers (Advanced Usage)

  • std::unique_ptr
  • std::shared_ptr
  • std::weak_ptr

Custom Deleter

std::unique_ptr<FILE, decltype(&fclose)> file(fopen("test.txt","r"), fclose);

Avoiding Cyclic References

Use std::weak_ptr to break ownership cycles.


4. Concurrency & Multithreading

๐Ÿ”น std::thread

std::thread t([](){ std::cout << "Hello\n"; });
t.join();

๐Ÿ”น Mutex

std::mutex m;
std::lock_guard<std::mutex> lock(m);

๐Ÿ”น std::async & Futures

auto future = std::async(std::launch::async, [](){ return 42; });
std::cout << future.get();

๐Ÿ”น Atomic Operations

std::atomic<int> counter(0);
counter++;

5. Advanced OOP Concepts

๐Ÿ”น Rule of Five

If you define one of:

  • Destructor
  • Copy constructor
  • Copy assignment
  • Move constructor
  • Move assignment
    You likely need all five.

๐Ÿ”น Virtual Table (vtable)

Used for runtime polymorphism.

๐Ÿ”น Multiple & Virtual Inheritance

class A {};
class B : virtual public A {};
class C : virtual public A {};
class D : public B, public C {};

Prevents diamond problem duplication.


6. Lambda Expressions (Advanced)

Capture by Move (C++14)

auto ptr = std::make_unique<int>(10);
auto lambda = [p = std::move(ptr)]() {
std::cout << *p;
};

Generic Lambdas (C++14)

auto add = [](auto a, auto b) { return a + b; };

7. STL Deep Concepts

๐Ÿ”น Custom Allocators

Control memory allocation strategy.

๐Ÿ”น Iterator Categories

  • Input
  • Output
  • Forward
  • Bidirectional
  • Random Access

๐Ÿ”น Algorithm Complexity

Understand Big-O of containers:

  • std::map โ†’ O(log n)
  • std::unordered_map โ†’ Average O(1)

8. RAII (Resource Acquisition Is Initialization)

Resource lifetime tied to object lifetime.

class File {
public:
File() { open(); }
~File() { close(); }
};

Core C++ design philosophy.


9. Expression Templates

Used in libraries like Eigen to avoid temporary objects and improve performance.


10. Advanced Memory Management

  • Placement new
  • Memory pools
  • Alignment (alignas, alignof)
  • Custom operator new/delete

11. Compile-Time Programming

๐Ÿ”น constexpr

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

๐Ÿ”น consteval (C++20)

Forces compile-time evaluation.


12. Design Patterns in C++

Common advanced patterns:

  • CRTP (Curiously Recurring Template Pattern)
  • Policy-based design
  • Type erasure (std::function, std::any)
  • Pimpl idiom
Leave a Reply 0

Your email address will not be published. Required fields are marked *