Add mstd::is_constant_evaluated
GCC 9 and sufficiently-new Clang (including ARM Compiler 6.13) give us
access to the C++20 (draft) `is_constant_evaluated` test. Allows us to
restrict code to compile-time only.

This is particularly useful when using assembler intrinsics, which the
compiler cannot optimise or compile-time evaluate. It allows us to write
something like

    constexpr uint32_t reverse_bits(uint32_t x)
    {
        if (is_constant_evaluated(x)) {
            // C code to do calculation here
            return x;
        } else {
            // an assembler intrinsic that cannot be optimised
            return __RBIT(x);
        }
    }

This can then be totally compile-time given a constant input.

(In this example, ultimately it would be a good idea to include this
functionality in CMSIS's GCC `__RBIT`, which needs it because GCC
requires use of assembler. Clang implements `__RBIT` as a built-in,
meaning it can already compute it at compile-time, so does not benefit.).
ls
1 parent 04f929e commit 07d43b72d26799f4db8918f32af74a59ef50260d
@Kevin Bracey Kevin Bracey authored on 19 Nov 2019
Showing 1 changed file
View
platform/cxxsupport/mstd_type_traits