Universal References (revisited) -- Ben Hekster

A response to Scott Meyers' recent article on Universal References, showing an analogy between &&-collapsing and const-collapsing, and making the counterargument that inventing a new concept may not be needed to clearly explain the standard. It's always interesting to see different experts' takes on how to understand and teach a  feature, particularly a new C++11 feature that we as a community are still absorbing.

Universal References

by Ben Hekster

... The ‘universal reference’ is not a concept you will see defined in the C++ standard, nor is it even something that has any conceptually objective existence in the language or compilation process. It is a construct defined by Meyers in an attempt to make some sense of behavior in the language that he presents as being unexpected or even mysterious. On closer inspection, however, I find that the observed mysterious behavior is actually quite readily explained and has an existing analog that corresponds to already intuitively-understood behavior. ...

Add a Comment

Comments are closed.

Comments (2)

0 0

Agentlien said on Apr 8, 2013 10:42 PM:

I agree with much of this article. It is essentially saying what I felt when I first read about universal references: It's a superfluos construct made up in order to explain something already well explained by reference collapsing. Originally, I didn't see the use, since I find reference collapsing an even clearer and rather simple rule.

However, Scott Meyers definitely deserves credit, because, judging from how other people have reacted to his article it definitely helped a lot of the people for whom reference collapsing was not intuitive. So, even though it's not a concept defined in the standard, it may be a valuable alternate view, or at least didactic tool, which can help those with a slightly different mindset.
1 0

Christian Rau said on Apr 9, 2013 12:10 PM:

I don't really see the neccessity for this article. The author seems more to restate the standard-view on the reference collapsing rules saying that he found Meyers' explanations unintuitive (which might indeed be true for someone completely acquainted with reference collapsing in the first place).

He argues that Meyers' definition of a universal reference is ambiguous, although this only applies to part of the definition when taken out of context. In the same way it argues that judged strictly by standard his explanations are plain wrong, which is, well, true of course, so what? Meyers' article was never meant as a formal standard ammendment, nobody would disagree that the term "universal reference" is just a conceptual idea and completely made up.

While this article's const-analogy helps to explain the general rule to always take the actual instantiated type into account when reasoning about templates, it achieves far less in explaining the move/forward ambiguity of rvalue references. While this ambiguity is rather implicit in the standard through the reference collpasing rules, it is of high practical relevance and explained pretty well by Meyers' mere conceptual distinction between "rvalue references" and "universal references".

But in the end it may be a subjective decision if you agree with Meyers' view or the author of this article, depending on how much you already know about reference collapsing and perfect forwarding yourself and how far you are willing to sacrifice adherence to strict standard wording for didactic purposes.