Usage of if constexpr in template

I am trying to understand the utility of if constexpr and want to know if there is any utility in using it in this way. template<bool B> int fun() { if constexpr (B) return 1; return 0; } Is this function changed at all by using if constexpr instead of a regular if? I assume… Read More Usage of if constexpr in template

How to return std::array of different size based on compile-time switch?

I need to create an array of static data, where the size (and data) is known at compile time, but differs between build configurations. This is a very dumbed-down version of what I’m trying to do (Please ignore glaring bad practices in this code as it is just an example): constexpr ProductType PRODUCT = ProductType::A;… Read More How to return std::array of different size based on compile-time switch?

How can I static assert to disallow "mixed endianness" in a non-templated member function

I am using 2 x std::uint64_t and 1 x std::uint32_t in a high performance implementation of of operator<=> in a struct conataining a std::array<std::byte, 20>. I am trying to make it cross compiler and architecture compatible. As part of that I am trying to outright reject any architecture with std::endian::native which is not std::endian::little or… Read More How can I static assert to disallow "mixed endianness" in a non-templated member function

C++ if constexpr vs template specialization

Consider these 2 examples Example 1 template<Type type> static BaseSomething* createSomething(); template<> BaseSomething* createSomething<Type::Something1>() { return Something1Creator.create(); } template<> BaseSomething* createSomething<Type::Something2>() { return Something2Creator.create(); } …. // other somethings Example2 template<Type type> static BaseSomething* createSomething() { if constexpr(type == Type::Something1) { return Something1Creator.create(); } else if constexpr(type == Type::Something2) { return Something2Creator.create(); } // Other… Read More C++ if constexpr vs template specialization