What is Proxy Class in C++?Ī proxy is a class that provides a modified interface to another class. Substitution Failure Is Not An Error is a language feature(not an idiom) a C++ compiler uses to filter out some templated function overloads during overload resolution. It is curiously recurring, isn't it? Why Curiously Recurring Template Pattern (CRTP) works? And that base class is a template specialization for the class A itself. What is the curiously recurring template pattern (CRTP)?ĬRTP occurs when a class A has a base class. It can also be used to handle multiple types of the return value from function/method(Although that's not recommended advice). By the way, do you know auto, or can you design one now? Best scenarios to apply type erasure idiom? Type erasure technique is used to design generic types that rely on the type of assignment (as we do in Python). You can apply return type resolver idiom when your input types are fixed but output types may vary. What does the compiler do now? When to use return type resolver idiom? 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. Why can't functions be overloaded by return type? set-up and clean-up), then that's the place you can employ RAII. When you have a set of steps to carry out a task, and two steps are ideal (i.e. Summary and FAQs When to actually use RAII?
Return Type Resolver can also used to provide a generic interface for assignment, independent of the object assigned to.You can also overcome the function overloading limitation on the basis of a return type as we have seen above.When you use nullptr (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.If you are unaware of constexpr, I have written a short post on when to use const vs constexpr in c++.
It thus ensures no resource leaks and provides a basic exception safety guarantee. RAII guarantees the release of resources at the end of a scope/destruction.Resource Acquisition Is Initialization idiom is a very powerful and vastly used idiom although the name is really terrible, as the idiom is rather about resource release than acquisition.Implementation: Wrap a 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 Īlso known as: Execute-around object, Resource release is finalization, Scope-bound resource management. Intent: To guarantee the release of resource(s) at the end of a scope. Note: There are also drawbacks to using some of these techniques which I have not discussed here or maybe I am not qualified enough. To explain them, I have taken a more pragmatic approach and weighed more on readability, simplicity over other fancy features, syntax sugar, and complexity. There are many other advanced C++ concepts and idioms other than the ones I list in this article, but I would consider these 7 as "should-knows". I have started updating myself with Modern C++ a while ago, and since my posts, " 21 new features of Modern C++ to use in your project" and " All about lambda function in C++" were popular, I decided to write about advanced C++ concepts and idioms that I have learned from this wikibook and course.