Some deduction examples about auto/decltype

lvalue

1
2
3
4
5
6
7
8
9
10
11
12
13
int i{0};

auto a = i; // int
decltype(auto) b = i; // int

auto c = (i); // int
decltype(auto) d = (i); // int&

auto& e = i; // int&
auto& f = (i); // int&

auto&& g = i; // int&
auto&& h = (i); // int&

rvalue

1
2
3
4
5
6
7
8
9
10
11
12
13
auto a = 1;            // int
decltype(auto) b = 1; // int

auto c = (1); // int
decltype(auto) d = (1); // int

// auto& e = 1; // wrong
// auto& f = (1); // wrong
const auto& e = 1; // const int&
const auto& f = (1); // const int&

auto&& g = 1; // int&&
auto&& h = (1); // int&&

xvalue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct Foo {};

auto a = Foo{}; // Foo
decltype(auto) b = Foo{}; // Foo

auto c = (Foo{}); // Foo
decltype(auto) d = (Foo{}); // Foo

// auto& e = Foo{}; // wrong
// auto& f = (Foo{}); // wrong
const auto& e = Foo{}; // const Foo&
const auto& f = (Foo{}); // const Foo&

auto&& g = Foo{}; // Foo&&
auto&& h = (Foo{}); // Foo&&

Some deduction examples about auto/decltype
http://wasprime.github.io/Dev/C++/Miscellaneous/Some-deduction-examples-about-auto-decltype/
Author
wasPrime
Posted on
May 11, 2023
Updated on
May 11, 2023
Licensed under