C++ check if statement can be evaluated constexpr

Here’s another solution, which is more generic (applicable to any expression, without defining a separate template each time). This solution leverages that (1) lambda expressions can be constexpr as of C++17 (2) the type of a captureless lambda is default constructible as of C++20. The idea is, the overload that returns true is selected when … Read more

Concatenate compile-time strings in a template at compile time?

You could use something like this. Everything happens at compile time. Specialize base_typename_struct to define your primitive types. template <const char* str, int len, char… suffix> struct append { static constexpr const char* value() { return append<str, len-1, str[len-1], suffix…>::value(); } }; template <const char* str, char… suffix> struct append<str, 0, suffix…> { static const … Read more

“What happened to my SFINAE” redux: conditional template class members?

Firstly, C++11 did not carry forward boost’s disable_if. So if you’re going to transition boost code, you’ll need to use enable_if with a negated condition (or redefine your own disable_if construct). Secondly, for SFINAE to reach in and apply to the method level, those methods must be templates themselves. Yet your tests have to be … Read more

Constexpr if alternative

One of pre-C++17 ways is to use partial template specializations, like here: template <typename T, bool AorB> struct dummy; template <typename T, true> struct dummy { static void MyFunc() { FunctionA<T>(); } } template <typename T, false> struct dummy { static void MyFunc() { FunctionB<T>(); } } template <typename T> void Facade() { dummy<T, MeetsConditions<T>::value>::MyFunc(); … Read more

Generating one class member per variadic template argument

As you have already been hinted, the best way is to use a tuple: template<typename …AcceptedTypes> // e.g. MyClass<T1, T2> class MyClass { std::tuple<std::vector<AcceptedTypes>…> vectors; }; This is the only way to multiply the “fields” because you cannot magically make it spell up the field names. Another important thing may be to get some named … Read more

Compile-time constant id

This is sufficient assuming a standards conforming compiler (with respect to the one definition rule): template<typename T> class A { public: static char ID_storage; static const void * const ID; }; template<typename T> char A<T>::ID_storage; template<typename T> const void * const A<T>::ID= &A<T>::ID_storage; From the C++ standard 3.2.5 One definition rule [basic.def.odr] (bold emphasis mine): … Read more

constexpr initialization of array to sort contents

It’s ugly, and probably not the best way to sort in a constant expression (because of the required instantiation depth).. but voilĂ , a merge sort: Helper type, returnable array type with constexpr element access: #include <cstddef> #include <iterator> #include <type_traits> template<class T, std::size_t N> struct c_array { T arr[N]; constexpr T const& operator[](std::size_t p) const … Read more