C++17


code
Definition Clamp is an algorithm added in C++17 that is used to limit a value in a given range.  If value > max, value is set to max If value < min, value is set to min Syntax Before C++17, we were relying on the following code (or similar) to […]

Library Feature: std::clamp


code
Syntax [crayon-5d5e8c2ab7fe5451408716/]   Tuples and data fetching Tuples are suggested for returning multiple values from a function. They are in a strong relation with std::tie, which is used for retrieving the values. The only problem is that we have to have all those variables previously declared. [crayon-5d5e8c2ab7ff0595068897/] But what if […]

Language feature: Structured binding


code
Syntax [crayon-5d5e8c2ab88d9700432252/] Invoke gives a uniform way of calling methods and functions. But why would we use this ? Let’s try to call some class functions, assuming we have the following struct defined. [crayon-5d5e8c2ab88e3212478916/] Before invoke: [crayon-5d5e8c2ab88e8613992178/] After invoke: [crayon-5d5e8c2ab88ec462654595/] This is pretty handy when you would like / need […]

Library feature: std::invoke



code
Syntax [crayon-5d5e8c2ab8d7a812066713/] Attributes are something that provides some warnings or restraints to the compiler, and inform the reader of the specific situation. The following attributes were defined in C++14 and C++17: [crayon-5d5e8c2ab8d84725956177/] Deprecated [crayon-5d5e8c2ab8d89140174512/] Indicates that the use of the name or entity declared with this attribute is allowed, but […]

Language feature: Attributes


code
Syntax [crayon-5d5e8c2ab92da070221638/] std::optional behaves as a variant of a type T and the value std::nullopt. We can set the value as we would do with any other type, but we can also assign it to std::nullopt – instead of returning nullptr, -1, 0, “”, or whatever we’d expect to receive […]

Library feature: std::optional


code
Nested namespaces is a tiny feature but i find it very useful. Before C++17, using multi-level namespaces was irritating both to read and to write. [crayon-5d5e8c2ab98d5643833988/] We could have improve the indentation by having everything in one line, but that looks horrible as well. [crayon-5d5e8c2ab98df176002798/] Luckily, C++17 now let’s you […]

Language feature: Nested namespaces




code
Syntax [crayon-5d5e8c2aba0a9109330574/] std::variant is a modern, safer alternative to the old unions. What are the differences? std::variant is type-safe: the variant knows what type it currently holds – but needs more memory space in order to do so std::variant calls the destructor of the object when it changes its type […]

Library feature: std::variant




code
Syntax [crayon-5d5e8c2abaa8c779944089/] constexpr Before discussing about if-constexpr, we should discuss first about constexpr. It was introduced in C++11 to mark an expression as having a compile-time constant result. More about constexpr can be read here. if-constexpr if-constexpr simply add if statements at compile time. This way, we can get rid […]

Language feature: Compile-time if statement / if-constexpr