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?

Sadeq
  • 7,795
  • 4
  • 34
  • 45
  • Because the way you use a const rhand reference would be identical to the way you use a const lhand reference in most cases. – IdeaHat Jul 15 '14 at 16:02

1 Answers1

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&.

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • If it is useless, why compiler doesn't forbid `const T&&`? Shouldn't it be forbidden? – Sadeq Jul 15 '14 at 16:12
  • 4
    @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". – Daniel Frey Jul 15 '14 at 16:14
  • @Sadeq, checkout https://stackoverflow.com/questions/4938875/do-rvalue-references-to-const-have-any-use – Qingchuan Zhang Mar 12 '23 at 13:59