通用参考(即"前向参考",c++标准名称)和完善的转发c++11,c++14具有许多重要优势; 看到这里,在这里.
在上面引用的Scott Meyers的文章(链接)中,根据经验表明:
如果变量或参数声明为某个推导类型 T的类型为T &&,则该变量或参数是通用引用.
实际上,使用clang ++我们看到以下代码片段将成功编译-std=c++14:
#include <utility>
template <typename T>
decltype(auto) f(T && t)
{
return std::forward<T>(t);
}
int x1 = 1;
int const x2 = 1;
int& x3 = x1;
int const& x4 = x2;
// all calls to `f` result in a successful
// binding of T&& to the required types
auto r1 = f …Run Code Online (Sandbox Code Playgroud)