Modern C++ Features
C++14 Features
Learn features introduced in the C++14 standard, refining C++11 capabilities.
Interview: Generic lambdas, return type deduction, and the introduction of std::make_unique.
Released as a refinement standard, C++14 completes features introduced in C++11. Key features include generic lambdas, function return type deduction, and std::make_unique.
Generic Lambdas
Allows lambda parameters to be declared as auto, generating template function calls under the hood.
std::make_unique
Resolves an omission in C++11. Provides a safe way to instantiate std::unique_ptr without using the new operator.
Digit Separators
Use single quotes as digit separators (e.g. 1'000'000) to improve code readability.
The Import of std::make_unique
While C++11 introduced std::make_shared, it did not include std::make_unique. Using raw new statements can lead to memory leaks if exception propagation occurs during parameter evaluation:
Safety Rule
Always use std::make_unique to construct unique pointers. It guarantees exception safety and avoids raw new calls:
auto ptr = std::make_unique<Widget>();
Code Walkthrough
Demonstrates generic lambdas, return type deduction, and binary literals in C++14.
#include <iostream> #include <memory> #include <vector>// C++14: Automatic return type deduction for functions auto calculateSum(int a, int b) { return a + b; // Compiler deduces return type is int }
int main() { // C++14: Binary literals and digit separators int mask = 0b1100'1010; long largeVal = 1'000'000'000L;
// C++14: Generic lambdas using auto parameters auto printVal = [](auto x) { std::cout << "Value: " << x << '\n'; };
printVal(42); // Deduces x as int printVal("Hello"); // Deduces x as const char*
// C++14: std::make_unique instantiation auto ptr = std::make_unique<std::vector<int>>(); ptr->push_back(10);
std::cout << "Vector size: " << ptr->size() << '\n'; return 0; }
Interview-Relevant Information
Q: Why was std::make_unique missing from C++11?
Answer: Its omission was an oversight by the committee. std::make_shared was prioritized because it combines control blocks and object allocations into a single memory block, which is a key optimization. Since unique pointers do not use control blocks, std::make_unique was considered less urgent but was added in C++14 to standardize smart pointer creation.
Q: How do generic lambdas work under the hood?
Answer: The compiler translates the lambda into a functor class with a templated call operator:
template<typename T> auto operator()(T x) const. This allows the lambda to accept any type.
Quick Checklist
Did you replace raw new smart pointer constructions with std::make_unique? Do you use generic lambdas? If yes, your code is C++14 compliant.
Use Cases
Replacing raw new/delete memory operations with smart pointers.
Writing reusable functional callbacks using generic lambdas.
Common Mistakes
Manually writing template classes when a generic lambda with auto parameters is simpler and cleaner.
Constructing std::unique_ptr instances using raw new, which can lead to memory leaks if constructor arguments throw exceptions.