Type erasure based on templates is a pretty sophisticated technique. It allows you to bridge dynamic polymorphism (object orientation) with static polymorphism (templates).
In my last post, I presented a possible std::advance implementation based on tag dispatching. One of my readers mentioned that I could also implement std::advance based on constexpr if, or concepts. His right. So let's do it.
Tag Dispatching enables it to choose a function based on the type characteristics. This decision takes place at compile time and is based on traits.
Thanks to templates, there are new ways of software design. Policies and traits are two commonly used idioms in C++.
Expression templates are typically used in linear algebra and are "structures representing a computation at compile-time, which structures are evaluated only as needed to produce efficient code for the entire computation" (https://en.wikipedia.org/wiki/Expression_templates). In other words, expression templates are only evaluated when needed.
In my previous post "More about Dynamic and Static Polymorphism", I used the Curiously Recurring Template Pattern (CRTP) to implement static polymorphism. Another typical use case for CRTP are mixins.
In my last post "Dynamic and Static Polymorphism", I introduced dynamic polymorphism. Today, I continue with static polymorphism and present are very interesting idiom in C++: curiously recurring template pattern (CRTP).
Polymorphism is the property that different types support the same interface. In C++, we distinguish between dynamic polymorphism and static polymorphism.
In today's post, I want to introduce a very interesting C++17 feature: constexpr if. constexpr if enables it to conditionally compile source code and can also be used for nice tricks at compile time.
With C++20, constexpr became way more powerful. Additionally, we have consteval functions in C++20 that are quite similar to constexpr functions.
Hunting
Today 2238
Yesterday 5317
Week 2238
Month 146409
All 11627563
Currently are 224 guests and no members online
Kubik-Rubik Joomla! Extensions
Read more...
Read more...