Usually when a function returns boost::optional
I've seen a lot of people returning an empty brace {}
to designate an empty value, that works fine and is shorter than returning boost::none
.
I tried to do something similar to empty a boost::optional<int>
, but when calling the copy assignment operator (or most probably the move assignment op) with an empty brace in the right side, the empty brace is converted to an int and then that value is assigned to the optional, so I end up with the variable set to 0 and not an empty value as I was expecting. Here's an example https://godbolt.org/g/HiF92v, if I try the same with std::experimental::optional
I get the result I'm expecting (just replace with std::experimental::optional in the example and you will see that the instruction becomes mov eax, eax
).
Also if I try with a different template argument for the boost optional (a non integer type) some compilers compile (with the behavior I'm expecting, an example here http://cpp.sh/5j7n) and others don't. So even for the same lib the behavior is different according to the template arg.
I'd like to understand what is going on here, I know it has something to do with the fact that I'm using a C++14 feature for a library that doesn't consider that into the design. I read the boost/optional
header but I got lost in the details, I also tried to study the compiled code without inlining with a similar result.
I'm using gcc 4.9.2 with -std=c++14 and boost 1.57.
btw: I known I should have used boost::optional::reset
or boost::none
, but I was trying to be consistent with the semantics in the rest of the code base.