I am looking at 8.4.4.1 in n4713 C++ standard:
void f() {
float x, &r = x;
[=] {
decltype(x) y1; // y1 has type float
decltype((x)) y2 = y1; // y2 has type float const& because this lambda is not mutable and x is an lvalue
decltype(r) r1 = y1; // r1 has type float&
decltype((r)) r2 = y2; // r2 has type float const&
};
}
The standard says r2 has type float const&
, and then I tried to print out the types:
#include <iostream>
template <class T>
void print_type() {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
int main () {
float x, &r = x;
[=] {
print_type<decltype(x)>();
print_type<decltype((x))>();
print_type<decltype(r)>();
print_type<decltype((r))>();
}();
}
It gave:
void print_type() [with T = float]
void print_type() [with T = const float&]
void print_type() [with T = float&]
void print_type() [with T = float&]
The type of r2 is float&
instead of float const&
. Did I do something wrong?
I used g++ 8.3.1 to compile. I also tried 9.1.0 but same thing.