Universal References and the Copy Constructor -- Eric Niebler

The "universal references" term is getting traction:

Universal References and the Copy Constructor

by Eric Niebler

From the article:

At the most recent NWCPP meeting in Redmond, WA, the always-entertaining Scott Meyers shared his latest insights about so-called “universal references” and their pitfalls. In particular, he was warning about the hazards of overloading on universal references. His advice was good, I thought, but missed some important corner cases about the interactions between universal references and the special member functions. In this article, I show what the “special” problems are with the special member functions and universal references, and some ways to avoid the problems. ...

... 

Scott’s advice is simple and sound: avoid overloading on universal references. By which he means, don’t do this:
template<typename T>
void foo( T const & t )
  {/*...*/}

template<typename T>
void foo( T && t )
  {/*...*/}

In the code above, the author presumably wanted all lvalues to go to the first and all rvalues to go to the second. But that’s not what happens. What happens is this: ...

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.