通用引用的语法 [英] Syntax for universal references

查看:117
本文介绍了通用引用的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个右值引用:

void foo(int&& a);

它不绑定到左值:

int i = 42;
foo(i);   // error

这是一个通用参考:

template<typename T>
void bar(T&& b);

它绑定到右值,它也绑定到左值:

It binds to rvalues and it also binds to lvalues:

bar(i);   // okay

这是一个右值引用:

template<typename T>
struct X
{
    void baz(T&& c);
};

它不绑定到左值:

X<int> x;
x.baz(i);   // error

为什么通用引用使用与右值引用相同的语法?这不是不必要的混乱的来源吗?委员会是否考虑过其他语法,例如 T&&&& T& * T @ T& 42 (只是最后一个开玩笑)?

Why do universal references use the same syntax as rvalue references? Isn't that an unnecessary source of confusion? Did the committee ever consider alternative syntaxes like T&&&, T&*, T@ or T&42 (just kidding on that last one)? If so, what were the reasons for rejecting alternative syntaxes?

推荐答案

一个通用的引用,例如 T& & 可以推导出 T 对象类型 em>

A universal reference such as T&& can deduce T to be an "object type", or a "reference type"

在您的示例中,它可以推导出 T int 当传递一个右值时,因此函数参数是 int&&& ,或者它可以推导 T int& 当传递一个左值时,在这种情况下函数参数是 int& (因为参考折叠规则说 std :: add_rvalue_reference< int&> :: type 只是 int&

In your example it can deduce T as int when passed an rvalue, so the function parameter is int&&, or it can deduce T as int& when passed an lvalue, in which case the function parameter is int& (because the reference collapsing rules say std::add_rvalue_reference<int&>::type is just int&)

如果 X 不能推导出 T > example),那么它不能被推导为 int& ,因此引用不是一个通用引用。

If T isn't deduced by the function call (as in your X::baz example) then it can't be deduced to int&, so the reference isn't a universal reference.

所以IMHO真的不需要新的语法,它很好地适合模板参数推导和参考崩溃规则,与小调整,模板参数可以推导为参考类型(在C ++ 03中的函数模板参数 T T& 类型将始终推导 T 一个对象类型。)

So IMHO there's really no need for new syntax, it fits nicely into template argument deduction and reference collapsing rules, with the small tweak that a template parameter can be deduced as a reference type (where in C++03 a function template parameter of type T or T& would always deduce T as an object type.)

这些语义和这种语法是从一开始就提出的,当推荐引用rvalue和调整参数推导规则作为转发的解决方案问题,请参阅 N1385 。使用这种语法提供完美的转发被提议与为移动语义的目的的提议的右值引用并行: N1377 在与N1385相同的邮件中。我不认为一个替代的语法被严重提出。

These semantics and this syntax were proposed right from the beginning when rvalue references and a tweak to the argument deduction rules were proposed as the solution to the forwarding problem, see N1385. Using this syntax to provide perfect forwarding was proposed in parallel with proposing rvalue references for the purposes of move semantics: N1377 was in the same mailing as N1385. I don't think an alternative syntax was ever seriously proposed.

IMHO替代语法实际上会更混乱。如果你有 template< typename T> void bar(T& @)作为通用引用的语法,但具有与我们今天相同的语义,那么当调用 bar(i)模板参数 T 可以推导为 int& int ,函数参数的类型为 int& int&&& ... T& @ (无论是什么类型。)所以你会用语言的语法来声明 T& @ 这不是一个可以存在的类型,因为它实际上总是指一些其他类型, int& int&

IMHO an alternative syntax would actually be more confusing anyway. If you had template<typename T> void bar(T&@) as the syntax for a universal reference, but the same semantics as we have today, then when calling bar(i) the template parameter T could be deduced as int& or int and the function parameter would be of type int& or int&& ... neither of which is "T&@" (whatever that type is.) So you'd have grammar in the language for a declarator T&@ which is not a type that can ever exist, because it actually always refers to some other type, either int& or int&&.

至少使用语法,我们得到类型 T&&< / code >是一个真实类型,参考折叠规则不是特定于使用通用引用的函数模板,它们与模板外的类型系统完全一致:

At least with the syntax we've got the type T&& is a real type, and the reference collapsing rules are not specific to function templates using universal references, they're completely consistent with the rest of the type system outside of templates:

struct A {} a;
typedef A& T;
T&& ref = a;    // T&& == A&

或等同于:

struct A {} a;
typedef A& T;
std::add_rvalue_reference<T>::type ref = a;    // type == A&

T 是一个左值引用类型时, T&&& 也是。我不认为需要一个新的语法,规则真的不复杂或混乱。

When T is an lvalue reference type, T&& is too. I don't think a new syntax is needed, the rules really aren't that complicated or confusing.

这篇关于通用引用的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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