c++typesstandardstype-systems

Why is decltype(ConstInt{}) not const?


Consider the following code:

#include <concepts>
#include <string>

using ConstInt = int const;
using ConstStr = std::string const;

static_assert(std::same_as<std::string const, decltype(ConstStr{})>); // true
static_assert(std::same_as<int const, decltype(ConstInt{})>);         // false!!!
static_assert(std::same_as<int, decltype(ConstInt{})>);               // true

int main() {
}

Note that: static_assert(std::same_as<int const, decltype(ConstInt{})>); // false!!!

See also: https://godbolt.org/z/fPKhdbrj1

Why is decltype(ConstInt{}) not const?


Solution

  • Because that's the type of the functional cast expression ConstInt{}.

    [expr.type]

    2 If a prvalue initially has the type “cv T”, where T is a cv-unqualified non-class, non-array type, the type of the expression is adjusted to T prior to any further analysis.