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_ptrstd::shared_ptrstd::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