什么是对函数类型的右值引用? [英] What is an rvalue reference to function type?

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

问题描述

我最近将思想集中在C ++ 0x的glvalues,xvalues和prvalues以及rvalue引用的概念上。但是,还有一件事让我难以理解:

I have recently wrapped my mind around the C++0x's concepts of glvalues, xvalues and prvalues, as well as the rvalue references. However, there's one thing which still eludes me:

什么是对函数类型的右值引用 ?草案中多次提到它。为什么引入这样的概念?

What is "an rvalue reference to function type"? It is literally mentioned many times in the drafts. Why was such a concept introduced? What are the uses for it?

推荐答案

我讨厌循环,但对函数类型的右值引用是对功能类型。有诸如函数类型之类的东西。 void()。您可以形成对此的右值引用。

I hate to be circular, but an rvalue reference to function type is an rvalue reference to function type. There is such a thing as a function type, e.g. void (). And you can form an rvalue reference to it.

根据 N3055 ,它是一个xvalue。

In terms of the classification system introduced by N3055, it is an xvalue.

它的用途是罕见且晦涩的,但并非没有用。例如:

Its uses are rare and obscure, but it is not useless. Consider for example:

void f() {}
...
auto x = std::ref(f);

x 具有类型:

std::reference_wrapper<void ()>

如果您查看 reference_wrapper 其中包括:

reference_wrapper(T&) noexcept;
reference_wrapper(T&&) = delete; // do not bind to temporary objects

在此示例中, T 是函数类型 void()。因此,第二个声明形成对函数类型的右值引用,以确保 reference_wrapper 不能使用右值参数构造。即使 T 是常量也不是。

In this example T is the function type void (). And so the second declaration forms an rvalue reference to function type for the purpose of ensuring that reference_wrapper can't be constructed with an rvalue argument. Not even if T is const.

如果形成对函数的右值引用不合法,则此即使我们没有将右值 T 传递给构造函数,保护也会导致编译时错误。

If it were not legal to form an rvalue reference to function, then this protection would result in a compile time error even if we did not pass an rvalue T to the constructor.

这篇关于什么是对函数类型的右值引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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