r值引用和指针之间的确切对应关系? [英] Exact correspondence between r-value references and pointers?

查看:169
本文介绍了r值引用和指针之间的确切对应关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有关C ++语言中的指针和引用类型之间的对称性的一般问题.

此通信表在C ++(C ++ 11及更高版本)中有意义吗?

  + ----------- + ---------------- +|参考|指针| ----------- | ---------------- ||夯;|T * const ||常数|T const * const ||T&&|??? * |+ ----------- + ---------------- + 

,如果有的话,与 ??? * ?对应的是什么.(是否缺少其他行?)

( T& 是针对具体类型 T 的,而不是推导的类型.有关尾随的 const ,请参见@ComicSansMS答案.)

我猜这将与 std :: move_iterator< T*> ,但这不是内置的语言功能,而且似乎还会产生许多其他问题(例如 std :: reference_wrapper 确实如此).

语言是否缺少某种r值指针以使其更加对称?更好的说法是在取消引用时生成r值的内容(例如,移动的对象-甚至是副本).

我知道r值引用对应于不具有名称"的事物,但是,如果仅通过遵循操作 * 获得相同的内容,这似乎并没有矛盾性.


注意1:

我看到 std :: move_iterator< It> ::在C ++ 20中不推荐使用operator->() .我不确定这意味着什么,也许这意味着它有时无法实现.这个问题与这种弃用有关,因为原则上 std :: move_iterator< It> :: operator->()可以返回 ??? * 而不是指针.(或返回 move_iterator< pointer> ,尽管这将生成->()运算符的无限回归……除非存在用于r值的真正指针以指向终止.)


注意2:我可以想象的一个候选缺失行是:

  |无效(不是东西)|无效* | 

但是我认为这与问题中所问的对应关系无关,而是语言中 void 的正常不规则之处.

解决方案

没有指针和引用类型之间的对称".或至少没有打算成为一个.您可以根据自己的喜好来解释语言,但是看到的任何对称性都是偶然的.

在大多数情况下,左值和右值引用的工作方式相同.主要区别在于您可以按类型区分它们(因此可以重载函数或基于它们来影响模板实例化),并且它们对绑定的对象具有不同的规则,尤其是在重载解析下.区别的目的是使类型系统更准确地传达源对象的值类别,以便用户可以更多地了解对引用对象进行合理的处理.

指针无需执行此操作,因此无需进行区分.

This is a general question about symmetry between pointer and reference types in the C++ language.

Is this table of correspondence meaningful in C++ (C++11 and beyond)?

+-----------+----------------+
| Reference | Pointer        |
|-----------|----------------|
| T&        | T* const       |
| T const&  | T const* const |
| T&&       | ???*           |
+-----------+----------------+

and if some, what would correspond to ???* ?. (Any additional rows are missing?)

(T&& is for a concrete type T, not a deduced type. See @ComicSansMS answer for the trailing const.)

My guess it will correspond to something like std::move_iterator<T*>, but it is not a built-in language feature and it seems to create a bunch of other problems (like std::reference_wrapper does).

Is the language missing some kind of r-value pointer to be more symmetric? Or better said something that generates r-values on derreference (for example a moved object -- or a even a copy).

I know that r-value references correspond to thing that "do not have a name", however that doesn't seem to be constradictory if that samething is only obtained for a deference operation *.


NOTE 1:

I see std::move_iterator<It>::operator->() is deprecated in C++20. Which I am not sure what it means, perhaps it means that it can't be implemented, optionally, sometimes. This question is relevant to this deprecation because in principle std::move_iterator<It>::operator->() could return ???* rather than pointer. (Or return move_iterator<pointer>, although that will generate a infinite regress of ->() operators... unless there is a true pointer for r-values to terminate.)


NOTE 2: A candidate missing row missing that I can imagine is:

| void& (not a thing)  | void*     |

but I think it is not related to the correspondence asked in the question, but a normal irregularity of void in the language.

解决方案

There is no "symmetry between pointer and reference types". Or at least, there isn't intended to be one. You can interpret the language however you like, but any symmetry you see there is accidental.

In most ways lvalue and rvalue references work the same way. The principal differences are that you can distinguish between them by type (and can therefore overload functions or affect template instantiation based on them), and they have different rules for what objects they bind, particularly under overload resolution. The purpose of the distinction is to make the type system more accurately convey the value category of the source object, so that the user can know more about what is reasonable to do with the referenced object.

Pointers don't need to do this, so they don't need to make this distinction.

这篇关于r值引用和指针之间的确切对应关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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