Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 820
xmllmx Avatar asked Jun 13 '26 01:06

xmllmx


1 Answers

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.

like image 78
StoryTeller - Unslander Monica Avatar answered Jun 15 '26 14:06

StoryTeller - Unslander Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!