boost :: optional vs boost :: make_optional [英] boost::optional vs boost::make_optional
问题描述
我想更好地理解使用 default 构造函数创建 boost :: optional 对象之间的区别:
I would like to understand better the difference between creating a boost::optional object using the default constructor:
boost::optional<PastaType> pasta = boost::optional<PastaType>(spaghetti)
或使用 make_optional 版本:
boost::optional<PastaType> pasta = boost::make_optional<PastaType>(spaghetti)
环顾四周,我只是理解使用 make_optional 版本PastaType不能是引用类型,但是我想弄清楚何时使用其中一种.
Looking around I just understoood that with the make_optional version PastaType cannot be a reference type, but I would like to figure out better when to use one or the other.
谢谢!
推荐答案
make_optional
是一种便利或帮助功能,可以通过推断的模板参数来减少必须编写的代码量>可选
.在功能上,这两种方法是等效的.
make_optional
is a convenience or helper function that can reduce the amount of code you have to write by inferring the template parameter of optional
. Functionally the two methods are equivalent.
auto pasta = boost::make_optional(spaghetti);
这篇关于boost :: optional vs boost :: make_optional的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!