Why doesn't C++ have a const universal reference?
Asked Answered
T

1

6

Scott Meyers says (for parameters to function templates):

Universal references can only occur in the form "T&&"! Even the simple addition of a const qualifier is enough to disable the interpretation of "&&" as a universal reference.

Why doesn't C++ have a const universal reference? Any technical reason?

Tupler answered 15/7, 2014 at 16:0 Comment(1)
Because the way you use a const rhand reference would be identical to the way you use a const lhand reference in most cases.Brynn
D
11

What would a const universal reference be? It would be a reference that can not be modified. And moving from an rvalue-reference is a modification. Therefore, if there is such a thing as a const universal reference, it is simply const T&.

Daughterinlaw answered 15/7, 2014 at 16:5 Comment(3)
If it is useless, why compiler doesn't forbid const T&&? Shouldn't it be forbidden?Tupler
@ccsadegh It's not useless, you might need it in some rare and complicated cases of overload resolution to remove ambiguity. It's just not useful for forming a "const universal reference".Daughterinlaw
@Sadeq, checkout #4939375Glinys

© 2022 - 2024 — McMap. All rights reserved.