右值引用功能 [英] rvalue reference to function

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

问题描述

typedef void(&&RF)(void* p);

RF rf()
{
    return f;
}

int ay[10] = { 0 };

typedef int(&&RA)[10];

RA ra()
{
    return ay; // error
}

cout << is_lvalue_reference<decltype(rf())>::value << endl; // 1

C ++参考文献指出:对函数的右值引用是否将
视为左值

The C++ reference says "rvalue references to functions are treated as lvalues whether named or not".

但是我不明白对此的考虑是什么?我想也许函数的名称总是一个左值。因此,它必须保留其左值的属性,并确保将函数名称传递到可以调用它的任何位置,例如 rf()(NULL)。然后,我想到了数组名称。我认为它始终也是左值,因此我编写了上面的代码进行测试并得到错误。

But I can not understand what the considerations for this are? I guess that perhaps the name of function is always a lvalue. So it must keep its attribute of an lvalue and ensure passing the function name to anywhere it can be invoked, like rf()(NULL). Then the array name came unbidden to my mind. I think it is always a lvalue too, so I wrote the code above to test this and got a error.

谁能指出所有这些背后的真正原因?

Who can point out the real reason behind all of this?

推荐答案

N3055 简要讨论了函数右值引用的问题:

In N3055 the issue of rvalue references to functions is briefly discussed:


此外,右值引用(如传统的左值引用)可以绑定到函数。但是,将
右值参考返回值当作右值来对待,在语言中引入了函数右值
的新概念。以前没有这样的想法–在右值上下文
中使用的函数左值成为指向函数右值的指针,而不是函数右值–因此,当前的标准草案并未
描述此类右值如何被处理。特别是,根据函数左值指定函数调用和对函数
指针的转换,因此在当前措辞中未定义对
函数的右值引用的大多数合理用法。

In addition, rvalue references (like traditional lvalue references) can be bound to functions. Treating an rvalue reference return value as an rvalue, however, introduces the novel concept of a function rvalue into the language. There was previously no such idea – a function lvalue used in an rvalue context becomes a pointer-to-function rvalue, not a function rvalue – so the current draft Standard does not describe how such rvalues are to be treated. In particular, function calls and conversions to function pointers are specified in terms of function lvalues, so most plausible uses of rvalue references to functions are undefined in the current wording.

函数没有生存期或存储时间,因此左值/右值区分对它们没有意义。另一方面,如果允许函数右值存在,则必须解决引用的段落中讨论的问题。有鉴于此,在我看来,将所有函数值都强制为左值是一个合理的解决方案。

Functions don't have lifetime or storage duration, so the lvalue/rvalue distinction doesn't make sense for them. On the other hand, if you allow function rvalues to exist, you have to resolve the issues discussed in the quoted paragraph. In light of this, forcing all function values to be lvalues seems to me to have been a reasonable solution.

我想,另一种解决方案是禁止函数右值总之,任何尝试创建对函数类型的右值引用的尝试都会导致程序格式错误。我不知道是否考虑过这种方法,但是我猜想这会给通用编程带来不便。

Another solution, I suppose, would have been to ban function rvalues altogether, so any attempt to create an rvalue reference to function type would result in an ill-formed program. I don't know whether this approach was considered, but my guess is that it would cause inconveniences with generic programming.

另一方面,对于任何对象类型,包括数组类型,左值与右值之间有有意义的区别。因此,该语言禁止您将对象类型的右值引用绑定到对象类型的左值。我不确定您为什么对自己的代码无法编译感到惊讶。

On the other hand, for any object type, including array types, there is a meaningful distinction between lvalues and rvalues. So the language forbids you from binding an rvalue reference to object type to an lvalue of object type. I'm not sure why you're surprised that your code doesn't compile.

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

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