Does it make sense to combine optional with reference_wrapper?

前端 未结 2 1587
庸人自扰
庸人自扰 2021-02-05 04:09

It occurred to me that in C++ it is possible to use the type std::optional>. An object of this type is essentially a reference

2条回答
  •  臣服心动
    2021-02-05 05:03

    The main difference between std::optional> and T* is that with T* you have to think about who owns the memory that is pointed to.

    If a function returns T* you have to know if you are responsible for freeing the memory or someone else is. That's not something you have to be concerned with when it's a reference.

提交回复
热议问题