将可选参数与reference

编程入门 行业动态 更新时间:2024-10-24 13:23:44
本文介绍了将可选参数与reference_wrapper结合使用是否有意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想到在C ++中可以使用 std :: optional< std :: reference_wrapper< T>> 类型.这种类型的对象实质上是对 T 类型的对象的引用,或者是一个空值,即几乎是一个指针.我的问题:

It occurred to me that in C++ it is possible to use the type std::optional<std::reference_wrapper<T>>. An object of this type is essentially a reference to an object of type T or a null value, i.e., pretty much a pointer. My questions:

  • std :: optional< std :: reference_wrapper< T>> 和 T * 之间有什么概念上的区别吗?

  • Is there any conceptual difference between std::optional<std::reference_wrapper<T>> and T*?

    有实际区别吗?在某些情况下,建议在 T * 上选择 std :: optional< std :: reference_wrapper< T>> ?

    Is there any practical difference? Are there situations where it might be advisable to choose std::optional<std::reference_wrapper<T>> over T*?

    推荐答案

    std :: optional< std :: reference_wrapper< T>> 和 T * 之间在概念上有区别吗?

    Is there any conceptual difference between std::optional<std::reference_wrapper<T>> and T*?

    顾名思义,

    std :: optional<> 旨在在我们可以有值或根本没有任何值时使用.

    std::optional<>, as the name already suggest, is meant to be used when we could have a value or might not have any value at all.

    等效于无值的 T * 对象将为其分配 nullptr ,即:指针将指向无处,而不是某处(甚至任何地方,即:未初始化).可以说, std :: optional<> 导出 nullptr 的概念以指向任何任意类型的指针.因此,我想说它们在概念上非常相似,是 std :: option<> 方法的一种概括.

    The equivalent of having no value for a T* object would be assigning nullptr to it, i.e.: the pointer will point to nowhere, as opposed to somewhere (or even anywhere, i.e.: uninitialized). It can be said that std::optional<> exports the concept of nullptr for pointers to any arbitrary type. So, I would say they are conceptually very similar, being the std::option<> approach a kind of generalization.

    有什么实际区别吗?在某些情况下,建议在 T * 上选择 std :: optional< std :: reference_wrapper< T>> ?

    我能想到的大小. std :: optional<> 包含一个内部标志,用于指示值的存在/不存在,而对于 T * , nullptr 被编码直接作为指针可以存储的值之一.因此, std :: optional< std :: reference_wrapper< T>> 对象将大于 T * .

    I can think of the size. std::optional<> contains an internal flag for indicating the presence/absence of a value, whereas for T* the nullptr is encoded directly as one of the values the pointer can store. So a std::optional<std::reference_wrapper<T>> object will be larger than a T*.

    在安全性方面,与 T * 不同, std :: optional<> 提供了成员函数 value()如果没有值,则为异常(它提供了与 T * 一样的不安全的 operator *()).

    When it comes to safety, unlike T*, std::optional<> provides the member function value() which throws an exception if there is no value (it provides as well as the unsafe operator*() as T* does).

    此外,例如,使用 std :: optional< std :: reference_wrapper< T>> 代替 T * ,例如,函数的返回值可能会在一种更明确的方式,可能根本没有任何价值.

    Also, using std::optional<std::reference_wrapper<T>> instead of T* , for example, as a function's return value may indicate in a more explicit way that there might be no value at all.

  • 更多推荐

    将可选参数与reference

    本文发布于:2023-08-04 17:32:31,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1298251.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:可选   参数   reference

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!