默认的模板类型可以是通用引用吗? [英] Can a defaulted template type be universal reference?

查看:128
本文介绍了默认的模板类型可以是通用引用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是&&& 是否为通用参考?

  template< class Function = std :: greater< int> > void f(Function&& f = Function()); 
<$ class $> / code>是Scott Meyers编写的用于区分正则右值引用(即 int&& 和模板代码中的右值引用)的术语, code> T&&& 。这是很重要的,因为参考折叠规则与模板代码发挥作用,所以这个术语用于辅助教学。它被称为反转引用的原因是它可以绑定到任何东西。



请记住,在C ++语言中不是通用引用这样的东西, Function&& 。但是,在Scott Meyer的术语中, Function&& 是一个通用引用 / p>

In the following, is && a universal reference ?

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

解决方案

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.

这篇关于默认的模板类型可以是通用引用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆