Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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());
like image 495
Vincent Avatar asked Mar 28 '26 11:03

Vincent


1 Answers

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.

like image 80
Jesse Good Avatar answered Mar 30 '26 01:03

Jesse Good



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!