Why decltype of constexpr variable is failed ?
#include <cstdint>
#include <type_traits>
constexpr uint16_t foo(){ return 0;}
constexpr auto cv = foo();
auto v = foo();
static_assert( std::is_same< uint16_t, decltype(cv)>::value, "!"); // failed
static_assert( std::is_same< uint16_t, decltype(v) >::value, "!"); // success
decltype(entity)specifies the declared type of theentityspecified by this expression.Due to the
constexpr, (Aconstexprspecifier used in an object declaration impliesconst), yourcvvariable is of typeconst uint16_t.You know that
const uint16_tis different fromuint16_tthen your line:fail as this is expected.
The line
specifies that the function
foocan be evaluated at compile time but the function still returns auint16_t. That why on the linevis of typeuint16_tthen the lineworks as expected too.