将可选参数与reference_wrapper结合使用是否有意义? [英] Does it make sense to combine optional with reference_wrapper?

查看:65
本文介绍了将可选参数与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:

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

  1. 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_wrapper结合使用是否有意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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