Consider the following code:
#include <type_traits>
int main() {
const int& p = 42;
auto v1 = decltype(p){};
static_assert(std::is_same_v<decltype(v1), int>);
decltype(p) v2{};
static_assert(std::is_same_v<decltype(v2), const int&>);
// auto v3 = X(const int&)X {};
}
Type of v1
is deduced as int
. At the same time type of v2
is expectedly deduced as const int&
. I think the first step for v1
could be treated as adding one more type alias using T = decltype(p);
and then auto v4 = T{};
. How this expression (decltype(p){}
or T{}
) is treated by compiler? I know that {}
part is for instantiation, but how the resulting type of v1
is not a reference type?
Yet another question: is there a way to declare v3
variable of the same type as v1
using explicitly noted type const int&
(instead of decltype(p)
)?
Any links to standard would be appreciated.