So I have started updating myself with Modern C++ a while ago & since my post & was popular I decided to write about advanced C++ concepts & idioms which I have learned from this & . 21 new features of Modern C++ to use in your project All about lambda function in C++ wikibook course /!\: Originally published @ . www.vishalchovatiya.com There are many other advanced C++ concepts & idioms as well but I would consider these 7 as "should-know". To explain them, I have taken a more pragmatic approach than sophistication. And weighed more on readability, simplicity over other fancy features, syntax sugars and complexity. There are also drawbacks of using some of these techniques which I have not discussed here or maybe I am not qualified enough. Note: 1. RAII To guarantee the release of resource(s) at the end of a scope. Wrap resource into a class; resource acquired in the constructor immediately after its allocation; and automatically released in the destructor; resource used via an interface of the class; Execute-around object, Resource release is finalization, Scope-bound resource management. Intent: Implementation: Also known as: Problem esource cquisition s nitialization idiom is the most powerful & vastly used idiom although the name is really terrible as the idiom is rather about resource release than acquisition. R A I I RAII guarantee the release of resource at the end of a scope/destruction. It thus ensures no resource leaks and provides basic . exception safety guarantee resource( x, y) { << ; } ~resource() { << ; } }; { resource *ptr = resource( , ); x; :: << ; :: >> x; (x == ) ; (x < ) ; ptr; } { struct resource int int cout "resource acquired\n" cout "resource destroyed\n" void func () new 1 2 int std cout "Enter an integer: " std cin if 0 throw 0 // the function returns early, and ptr won't be deleted! if 0 return // the function returns early, and ptr won't be deleted! // do stuff with ptr here delete In the above code, the early or statement, causing the function to terminate without being deleted. return throw ptr Consequently, the memory allocated for variable is now leaked (and leaked again every time this function is called and returns early). ptr Solution < T* m_ptr; : < ... Args> smart_ptr(Args&&... args) : m_ptr( T( ::forward<Args>(args)...)){} ~smart_ptr() { m_ptr; } smart_ptr( smart_ptr& rhs) = ; smart_ptr& =( smart_ptr& rhs) = ; smart_ptr(smart_ptr&& rhs) : m_ptr(exchange(rhs.m_ptr, )){} smart_ptr& =(smart_ptr&& rhs){ (&rhs == ) * ; m_ptr; m_ptr = exchange(rhs.m_ptr, ); * ; } T& *() { *m_ptr; } T* ->() { m_ptr; } }; { ptr = smart_ptr<resource>( , ); } template > { class T class smart_ptr public template typename new std delete const delete operator const delete nullptr operator if this return this delete nullptr return this operator const return operator const return void func () auto 1 2 // now ptr guarantee the release of resource // ... Note that no matter what happens after declaration, will be destroyed when the function terminates (regardless of how it terminates). ptr ptr As the is a local , the destructor will be called while the rewinds. Hence, we are assured that the will be properly cleaned up. ptr object function stack frame resource Use cases Using RAII, resources like , , , mutex , file , count , database or anything else that exists in limited supply can easily be managed. new/delete malloc/free acquire/release lock/unlock open/close ++/-- connect/disconnect Examples from C++ Standard Library include , , , etc. std::unique_ptr std::ofstream std::lock_guard 2. Return Type Resolver To deduce the type of the object being initialized or assigned to. Uses templatized conversion operator. Return type overloading Intent: Implementation: Also known as: Issue { ::stoi(str); } { ::stof(str); } int from_string ( *str) const char return std float from_string ( *str) const char return std // error A function cannot be overloaded only by its return type. Solution m_str; : from_string( *str) : m_str(str) {} < type> { ; (is_same_v<type, >) stoi(m_str); } }; n_int = from_string( ); n_float = from_string( ); { class from_string const string public const char template typename operator type () if constexpr (is_same_v<type, >) float return stof (m_str) else if int return int "123" float "123.111" // Will only work with C++17 due to `is_same_v` If you are unaware of constexpr, I have written a short post on . when to use const vs constexpr in c++ Use cases When you use (introduced in C++11), this is the technique that runs under the hood to deduce the correct type depending upon the pointer variable it is assigning to. nullptr You can also overcome the function overloading limitation on the basis of a return type as we have seen above. Return Type Resolver can also used to provide a generic interface for , independent of the object assigned to. assignment 3. Type Erasure To create generic container that can handle a variety of concrete types. Intent: Can be implemented by void*, templates, polymorphism, union, proxy class, etc. Implementation: Also known as: Duck-typing Problem C++ is a language with strong typing. In statically typed languages, object type known & set at compile-time. While in dynamically typed languages the type associated with run-time values. statically typed In strongly typed languages the type of an object doesn't change after compilation. To overcome this limitation & providing a feature like dynamically typed languages, library designers come up with various generic container kind of things like (C++17), (C++17), (C++11), etc. std::any std::variant std::function Different Type Erasure Techniques There is no one strict rule on how to implement this idiom, it can have various forms with its own drawbacks as follows: => Type erasure using void* (like in C) ; void qsort ( * base, num, size, (*compare)( *, *)) void size_t size_t int const void const void not safe & separate compare function needed for each type Drawback: => Type erasure using templates < template > ( , ); class RandomAccessIterator void sort RandomAccessIterator first RandomAccessIterator last may lead to many function template instantiations & longer compilation time Drawback: => Type erasure using polymorphism = ; }; base { { << ; } }; base { { << ; } }; { ptr->method(); }; { struct base virtual void method () 0 : struct derived_1 void method () cout "derived_1\n" : struct derived_2 void method () cout "derived_2\n" // We don't see a concrete type (it's erased) though can dynamic_cast void call (base* ptr) run-time cost (dynamic dispatch, indirection, vtable, etc.) Drawback: => Type erasure using union }; U { Data d; :: n; c; ~U() {} }; { struct Data union // occupies 1 byte std int32_t // occupies 4 bytes char // occupies 1 byte // need to know currently active type // an instance of U in total occupies 4 bytes. not Drawback: type-safe Solution As I mentioned earlier that standard library already has such generic containers. To understand type erasure better let's implement one like : std::any }; < T> base{ inner(T t): { ::move(t)} {} T ; {} }; any(): m_ptr{ }, typePtr{ } {} < T> any(T && t): m_ptr{ ::make_unique<inner<T>>(t)}, typePtr{&inner<T>::type} {} < T> any& =(T&& t){ m_ptr = ::make_unique<inner<T>>(t); typePtr = &inner<T>::type; * ; } : < T> ; :: <base> m_ptr = ; (*typePtr)() = ; }; < T> { (var.typePtr == any::inner<T>::type) <any::inner<T>*>(var.m_ptr.get())-> ; ::logic_error{ }; } { ; :: << any_cast< >(var) << :: ; var = :: { }; :: << any_cast< :: >(var) << :: ; ; } { struct any { struct base template typename : struct inner m_t std m_t static void type () nullptr nullptr template typename std template typename operator std return this private template typename T& friend any_cast ( any& var) const std unique_ptr nullptr void nullptr template typename T& any_cast ( any& var) const if return static_cast m_t throw std "Bad cast!" int main () any var ( ) 10 std cout int std endl std string "some text" std cout std string std endl return 0 Especially, the thing here to note is how we are leveraging empty static method i.e. to determine template instance type in . inner<T>::type() any_cast<T> Use cases Employ to handle multiple types of the return value from function/method(Although that's not recommended advice). 4. CRTP To achieve static polymorphism. Intent: Make use of base class template specialization. Implementation: Upside-down inheritance, Static polymorphism Also known as: Problem <( value &rhs) { m_x < rhs.m_x;} : }; <( value &rhs) { m_x < rhs.m_x;} : }; ... ... { struct obj_type_1 bool operator const const return // bool operator==(const value &rhs) const; // bool operator!=(const value &rhs) const; // List goes on. . . . . . . . . . . . . . . . . . . . private // data members to compare { struct obj_type_2 bool operator const const return // bool operator==(const value &rhs) const; // bool operator!=(const value &rhs) const; // List goes on. . . . . . . . . . . . . . . . . . . . private // data members to compare { struct obj_type_3 { struct obj_type_4 // List goes on. . . . . . . . . . . . . . . . . . . . For each comparable objects, you need to define respective comparison operators. This is redundant because if we have an , we can overload other operators on the basis of it. operator < Thus, is the only one operator having type information, other operators can be made type independent for reusability purpose. operator < Solution uriously ecurring emplate attern implementation rule is simple, C R T P separate out the type-dependent & independent functionality and bind type independent functionality with the base class using template specialization . Above line may seem cryptic at first. So, consider the below solution to the above problem for more clarity: < }; compare<value> { value( x): m_x(x) {} <( value &rhs) { m_x < rhs.m_x; } : m_x; }; < ( < derived&>(rhs) < < derived&>(lhs)); } { value v1{ }, v2{ }; <<boolalpha<< << (v1 > v2) << ; ; } template > { class derived struct compare : struct value public const int bool operator const const return private int template > >( <derived> & , <derived> & ) { class derived bool operator const compare lhs const compare rhs // static_assert(std::is_base_of_v<compare<derived>, derived>); // Compile time safety measures return static_cast const static_cast const /* Same goes with other operators == :: returns !(lhs < rhs) and !(rhs < lhs) != :: returns !(lhs == rhs) >= :: returns (rhs < lhs) or (rhs == lhs) <= :: returns (lhs < rhs) or (rhs == lhs) */ int main () 5 10 cout "v1 == v2: " '\n' return 0 // Now no need to write comparator operators for all the classes, // Write only type dependent `operator <` & use CRTP Use cases CRTP widely employed for static polymorphism without bearing the cost of virtual dispatch mechanism. Consider the following code we have not used virtual keyword & still achieved the functionality of polymorphism(specifically static polymorphism). < specific_animal> { implementation().who(); } : { * <specific_animal*>( );} }; animal<dog> { { << << ; } }; animal<cat> { { << << ; } }; < specific_animal> { animal.who(); } template typename { struct animal void who () private specific_animal& implementation () return static_cast this : struct dog public void who () cout "dog" endl : struct cat public void who () cout "cat" endl template typename void who_am_i (animal<specific_animal> & animal) CRTP can also used for optimization as we have seen above it also enables code reusability. Update: The above hiccup of declaring multiple comparisons operator will permanently be sorted from C++20 by using ( )/ . spaceship <=> Three-way-comparison operator 5. Virtual Constructor To create a copy or new object without knowing its concrete type. Intent: Exploits overloaded methods with polymorphic assignment. Implementation: Factory method/design-pattern. Also known as: Problem C++ has the support of polymorphic object destruction using it's base class's . Equivalent support for creation and copying of objects is missing as С++ doesn't support virtual constructor, . virtual destructor copy constructors Moreover, you can’t create an object unless you know its static type, because the compiler must know the amount of space it needs to allocate. For the same reason, copy of an object also requires its type to known at compile-time. ~animal(){ << ; } }; animal { ~dog(){ << ; } }; animal { ~cat(){ << ; } }; { who; } { struct animal virtual cout "~animal\n" : struct dog cout "~dog\n" : struct cat cout "~cat\n" void who_am_i (animal *who) // not sure whether dog would be passed here or cat // How to `create` the object of same type i.e. pointed by who ? // How to `copy` object of same type i.e. pointed by who ? delete // you can delete object pointed by who Solution The Virtual Constructor technique allows polymorphic creation & copying of objects in C++ by delegating the act of creation & copying the object to the derived class through the use of virtual methods. Following code is not only implement virtual constructor(i.e. ) but also implements virtual (i.e. ) . create() copy constructor clone() ~animal() = ; :: <animal> create() = ; :: <animal> clone() = ; }; animal { :: <animal> create() { ::make_unique<dog>(); } :: <animal> clone() { ::make_unique<dog>(* ); } }; animal { :: <animal> create() { ::make_unique<cat>(); } :: <animal> clone() { ::make_unique<cat>(* ); } }; { new_who = who->create(); duplicate_who = who->clone(); who; } { struct animal virtual default virtual std unique_ptr 0 virtual std unique_ptr 0 : struct dog std unique_ptr return std std unique_ptr return std this : struct cat std unique_ptr return std std unique_ptr return std this void who_am_i (animal *who) auto // `create` the object of same type i.e. pointed by who ? auto // `copy` object of same type i.e. pointed by who ? delete // you can delete object pointed by who Use cases To provide a generic interface to produce/copy a variety of classes using only one class. 6. SFINAE and std::enable_if To filter out functions that do not yield valid template instantiations from a set of overloaded functions. Intent: Achieved automatically by compiler or exploited using std::enable_if. Implementation: Motivation ubstitution ailure s ot n rror is a language feature(not an idiom) a C++ compiler uses to filter out some templated function overloads during overload resolution. S F I N A E During overload resolution of function templates, when substituting the explicitly specified or deduced type for the template parameter fails, the specialization discarded from the overload set instead of causing a compile error. Substitution failure happens when type or expression is ill-formed. < { << ; } { << ; } } = ; var = ;} ; func(& ); func(& ); template > ( * ){ class T void func T t // Single overload set if constexpr ( ::is_class_v<T>) std cout "T is user-defined type\n" else cout "T is primitive type\n" int primitive_t 6 { struct char '4' class_t class_t primitive_t How can we create two sets(based on primitive type & user-defined type separately) of a function having the same signature? Solution < : < ::is_class_v<T>>> { << ; } < : < ::is_integral_v<T>, T> = > func(T* t){ << ; } template , = : class T typename std enable_if_t std void func (T* t) cout "T is user-defined type\n" template , : class T std enable_if_t std 0 void // function signature is NOT-MODIFIED NOTE: cout "T is primitive type\n" Above code snippet is a short example of exploiting SFINAE using , in which first template instantiation will become equivalent to and second, . std::enable_if void func<(anonymous), void>((anonymous) * t) void func(int * t) You can read more above . std::enable_if here Use cases Together with , SFINAE is heavily used in template metaprogramming. std::enable_if The standard library also leveraged SFINAE in most utilities. type_traits Consider the following: < T> < C> ; < C> ; : { value = (is_class_type<T>::test<T>( )) == ( ) }; }; }; { <<is_class_type< >::value<< ; <<is_class_type< >::value<< ; ; } // Stolen & trimmed from https://stackoverflow.com/questions/982808/c-sfinae-examples. template typename { class is_class_type template typename static char test ( C::*) int template typename static double test (...) public enum sizeof 0 sizeof char { struct class_t int main () cout class_t endl // 1 cout int endl // 0 return 0 Without SFINAE, you would get a compiler error, something like " cannot be converted to member pointer for non-class type " as both the overload of only differs in terms of the return type. 0 int test Because is not a class, so it can't have a member pointer of type . int int int::* 7. Proxy To achieve intuitive functionality using middleware class. Intent: By use of temporary/proxy class. Implementation: (i.e. subscript) proxy, double/twice operator overloading Also known as: operator [] Motivation Most of the dev believes this is only about the subscript operator (i.e. ), but I believe type that comes in between exchanging data is proxy. operator[ ] We have already seen a nice example of this idiom indirectly above in (i.e. class ). But still, I think one more example will add concreteness to our understanding. type-erasure any::inner<> operator [ ] solution < T = > struct arr2D{ : struct proxy_class{ proxy_class(T *arr) : m_arr_ptr(arr) {} T & []( idx) { m_arr_ptr[idx]; } : T *m_arr_ptr; }; T m_arr[ ][ ]; : arr2D::proxy_class []( idx) { arr2D::proxy_class(m_arr[idx]); } }; { arr2D<> arr; arr[ ][ ] = ; << arr[ ][ ]; ; } template typename int private operator uint32_t return private 10 10 public operator uint32_t return int main () 0 0 1 cout 0 0 return 0 Use cases To create intuitive features like double operator overloading, etc. std::any Summary by FAQs When to actually use RAII? When you have set of steps to carry out a task & two steps are ideal i.e. set-up & clean-up, then that's the place you can employ RAII. Why can't functions be overloaded by return type? You can't overload on return types as it is not mandatory to use the return value of the functions in a function call expression. For example, I can just say ; get_val() What does the compiler do now? When to use return type resolver idiom? You can apply return type resolver idiom when your input types are fixed but output types may vary. What is type erasure in C++? Type erasure technique is used to design generic type which relies on the type of (as we do in python). assignment By the way, do you know auto or can you design one now? Best scenarios to apply type erasure idiom? Useful in generic programming. Can also be used to handle multiple types of the return value from function/method(Although that's not recommended advice). What is the curiously recurring template pattern (CRTP)? CRTP is when a class A has a base class. And that base class is a template specialization for the class A itself. E.g. ; ; template <class T> class X{...} class A : public X<A> {...} It curiously recurring, isn't it? is Why Curiously Recurring Template Pattern (CRTP) works? I think answer is very appropriate. this What is SFINAE? ubstitution ailure s ot n rror is a language feature(not an idiom) a C++ compiler uses to filter out some templated function overloads during overload resolution. S F I N A E What is Proxy Class in C++? A proxy is a class that provides a modified interface to another class. Why do we not have a virtual constructor in C++? A virtual-table(vtable) is made for each Class having one or more 'virtual-functions'. Whenever an object is created of such class, it contains a 'virtual-pointer' which points to the base of the corresponding vtable. Whenever there is a virtual function call, the vtable is used to resolve to the function address. A constructor can not be virtual, because when the constructor of a class is executed there is no vtable in the memory, means no virtual pointer defined yet. Hence the constructor should always be non-virtual. Can we make a class copy constructor virtual in C++? Similar to "Why do we not have a virtual constructor in C++?" which already answered above. What are the use cases & need for virtual constructor? To create & copy the object(without knowing its concrete type) using a base class polymorphic method.