Detecting Endianness

As stated earlier, the only “real” way to detect Big Endian is to use runtime tests. However, sometimes, a macro might be preferred. Unfortunately, I’ve not found a single “test” to detect this situation, rather a collection of them. For example, GCC recommends : __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ . However, this only works with latest versions, … Read more

Is it possible to test if a constexpr function is evaluated at compile time?

C++20 introduces is_constant_evaluated, defined in header <type_traits>, which addresses this issue. constexpr int foo(int s) { if (std::is_constant_evaluated()) // note: not “if constexpr” /* evaluated at compile time */; else /* evaluated at run time */; } Note that here the ordinary if is used instead of if constexpr. If you use if constexpr, then … Read more

Is there any advantage in using static_cast rather than C-style casting for non-pointer types?

One advantage which the other two answers didn’t mention yet is that static_cast is much easier to spot. The meaning of parentheses is notoriously overloaded in C++ and it can be difficult to spot evil (or even incorrect) casts. When I see something ending in _cast though, it’s like a mental speed bump: I slow … Read more

Is is_constexpr possible in C++11?

I once wrote it (EDIT: see below for limitations and explanations). From https://stackoverflow.com/a/10287598/34509 : template<typename T> constexpr typename remove_reference<T>::type makeprval(T && t) { return t; } #define isprvalconstexpr(e) noexcept(makeprval(e)) However there are many kinds of constant expressions. The above answer detects prvalue constant expressions. Explanation The noexcept(e) expression gives false iff e contains a potentially … Read more

How do I switch/select types during compile-time?

This should work: template<std::size_t N, typename… T> using static_switch = typename std::tuple_element<N, std::tuple<T…> >::type; Another method: template<std::size_t N, typename T, typename… Ts> struct static_switch { using type = typename static_switch<N – 1, Ts…>::type; }; template<typename T, typename… Ts> struct static_switch<0, T, Ts…> { using type = T; };

Is std::less supposed to allow comparison of unrelated pointers at compile-time?

I don’t think there’s a clear answer to the question that you’re asking. This is a specific case of LWG 2833: marking a library function constexpr does not explain the circumstances under which calling the function will yield a constant expression. Until this issue is resolved, I think you simply cannot rely on std::less being … Read more

Initialize an std::array algorithmically at compile time

For completeness’ sake, here’s a version that does not require the definition of a function but instead uses a lambda. C++17 introduced the ability of using lambdas in constant expressions, so you can declare your array constexpr and use a lambda to initialize it: static constexpr auto axis = [] { std::array<double, num_points> a{}; for … Read more

How to reduce compile time with C++ templates

Several approaches: The export keyword could theoretically help, but it was poorly supported and was officially removed in C++11. Explicit template instantiation (see here or here) is the most straightforward approach, if you can predict ahead of time which instantiations you’ll need (and if you don’t mind maintaining this list). Extern templates, which are already … Read more

Can I obtain C++ type names in a constexpr way?

Well, you could, sort of, but probably not quite portable: struct string_view { char const* data; std::size_t size; }; inline std::ostream& operator<<(std::ostream& o, string_view const& s) { return o.write(s.data, s.size); } template<class T> constexpr string_view get_name() { char const* p = __PRETTY_FUNCTION__; while (*p++ != ‘=’); for (; *p == ‘ ‘; ++p); char const* … Read more