What are template deduction guides and when should we use them?

Template deduction guides are patterns associated with a template class that tell the compiler how to translate a set of constructor arguments (and their types) into template parameters for the class. The simplest example is that of std::vector and its constructor that takes an iterator pair. template<typename Iterator> void func(Iterator first, Iterator last) { vector … Read more

How to assert that a constexpr if else clause never happen?

You have to make the discarded statement dependent of the template parameters template <class…> constexpr std::false_type always_false{}; if constexpr(condition1){ … } else if constexpr (condition2) { …. } else if constexpr (condition3) { …. } else { static_assert(always_false<T>); } This is so because [temp.res]/8 – The program is ill-formed, no diagnostic required, if no valid … Read more

Deprecated header replacement

Don’t worry about that. According to the same information source: this library component should be retired to Annex D, along side , until a suitable replacement is standardized. So, you can still use it until a new standardized, more-secure version is done.

reinterpret_cast creating a trivially default-constructible object

There is no X object, living or otherwise, so pretending that there is one results in undefined behavior. [intro.object]/1 spells out exhaustively when objects are created: An object is created by a definition ([basic.def]), by a new-expression ([expr.new]), when implicitly changing the active member of a union ([class.union]), or when a temporary object is created … Read more

What are the new features in C++17?

Language features: Templates and Generic Code Template argument deduction for class templates Like how functions deduce template arguments, now constructors can deduce the template arguments of the class http://wg21.link/p0433r2 http://wg21.link/p0620r0 http://wg21.link/p0512r0 template <auto> Represents a value of any (non-type template argument) type. Non-type template arguments fixes template<template<class…>typename bob> struct foo {} ( Folding + … … Read more

How do inline variables work?

The first sentence of the proposal: ” The ​inline specifier can be applied to variables as well as to functions. The ¹guaranteed effect of inline as applied to a function, is to allow the function to be defined identically, with external linkage, in multiple translation units. For the in-practice that means defining the function in a header, that can be included in multiple translation units. The … Read more