Search code examples
c++templatesc++11referenceuniversal-reference

Can a defaulted template type be universal reference?


In the following, is && a universal reference ?

template <class Function = std::greater<int> > void f(Function&& f = Function());

Solution

  • The term universal reference is a made up term by Scott Meyers to make a distinction between normal rvalue references, i.e. int&& and rvalue references in template code, i.e. T&&. This is important because of the reference collapsing rules that come into play with template code, so this term is used to aid in teaching. The reason it is called a unversal reference is because it can bind to anything.

    Just keep in mind that there is no such thing as a universal reference in the C++ language, Function&& is a rvalue reference as far as the language is concerned. However, yes, in Scott Meyer's made up terminology, Function&& is a universal reference in your example.