What is the difference between const auto&&
and const auto&
in regard to forwarding references? How do they differ in binding?
Asked
Active
Viewed 92 times
1

Nicol Bolas
- 449,505
- 63
- 781
- 982

nowi
- 437
- 4
- 13
-
The technically correct terminology for "*universal reference*" is "*forwarding reference*", see [this question](https://stackoverflow.com/questions/39552272/is-there-a-difference-between-universal-references-and-forwarding-references). – walnut Apr 07 '20 at 10:07
1 Answers
0
What is the difference between const auto&& and const auto& in regard to universal references?
Their difference is that const auto&&
is an rvalue reference to const and const auto&
is an lvalue reference to const. What they have in common is that neither is a forwarding reference. Only auto&&
is a forwarding reference (or T&&
where T
is deduced).
const auto&&
does not bind to lvalues, unlike const auto&
and T&&
.

eerorika
- 232,697
- 12
- 197
- 326
-
-
1@nowi Essentially, the fact that it is not a forwarding reference is an advantage when you don't want to have a forwarding reference. Check [here](https://www.reddit.com/r/cpp/comments/bk8j8v/uses_of_const_t/emeu810?utm_source=share&utm_medium=web2x) for a list of cases where the standard uses `const &&`. It's quite obscure. – eerorika Apr 07 '20 at 10:23
-
Interesting note about `const &&`, I didn't know that and it's very useful, I may use that myself. But I believe I asked about `const auto &` in my comment and `auto &&`, not `const auto &&` – nowi Apr 07 '20 at 10:28
-
1@nowi Oh, I didn't notice :) The advantage of `auto &&` of course is that you can use it for perfect forwarding. – eerorika Apr 07 '20 at 10:32
-
-
@nowi Oh, I still misunderstood/misread the original comment :D `const auto &` is advantageous when you want to be certain that you don't end up modifying the referred object by accident. – eerorika Apr 07 '20 at 22:23
-
-
@nowi Reference is just indirection. They are all "the same" "under the hood". – eerorika Apr 07 '20 at 23:03