Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::optional vs boost::make_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)

or using the make_optional version:

boost::optional<PastaType> pasta = boost::make_optional<PastaType>(spaghetti)

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.

Thanks!

like image 981
giubacchio Avatar asked Oct 26 '25 04:10

giubacchio


1 Answers

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);
like image 196
Woodford Avatar answered Oct 27 '25 17:10

Woodford



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!