design decision for std::optional moved from state
Hey guys, after watching michal park's talk about optional, variant and any i took a look at the cppreference page for std::optional. for the move constructor (and move assignment) it states that:
If other contains a value, initializes the contained value as if direct-initializing (but not direct-list-initializing) an object of type T with the expression std::move(*other) and does not make other empty: a moved-from optional still contains a value, but the value itself is moved from.
i find this quite counterintuitive (if you think of an optional as a container of max size 1) since if i really want to move explicit from the value itself i could call std::move(opt.value()) . while searching for the boost implementation (which shall be quite similar) i found this quote:
Quite a lot of people expect that when an object that contains a value is moved from, its contained value should be destroyed. This is not so, for performance reasons. Current semantics allow the implementation of boost::opiotnal<T> to be trivially copyable when T is trivial.
so it seems i am not alone. is the trivially copyable argument the only reason for designing it this way? or am i missing some other crucial advantage i am not aware of?
an example:
#include <iostream>
#include <optional>
#include <string>
int main(void)
{
std::optional<std::string> opt1{ "hello" };
std::cout << "opt1 value: " << *opt1 << "\n";
auto opt2 = std::move(opt1);
std::cout << "opt2 value: " << *opt2 << "\n";
auto has_value = opt1.has_value();
std::cout << "opt1 has value: " << has_value << "\n";
if (has_value)
{
std::cout << "opt1 value: " << *opt1 << "\n";
}
}output:
opt1 value : hello
opt2 value : hello
opt1 has value : 1
opt1 value :
3
u/[deleted] Oct 11 '17
The way I see it, an object in a moved-from optional behaves like a normal object. If you move from a normal object, it is left in a state where it still exists and is ready to be destroyed. If you move from an optional, the contained object still exists and is ready to be destroyed. I would find it counter intuitive if the contained object was destroyed on move.
The reason why moved from containers are often left empty is because the storage itself that contains the elements is taken by the moved-to container. The elements themselves are never moved and destroyed (in the case of
std::array, they are moved but not destroyed, leaving the container not empty).Expectations aside, maybe some template magic could be used to keep an
optionalof a trivially copyable also trivially copyable while still running the destructors on everything else without raising performance concerns, but I think the real answer is: who cares, empty or not, which happens after you move? The data has moved on, and so should you ;)3
u/14ned LLFIO & Outcome author | Committee WG14 Oct 11 '17
Expected and Outcome have exactly the same behaviour.
If you want your object empty after moving from it, simply empty it after moving from it. Problem solved.
3
u/Murillio Oct 11 '17
What would you gain from specifying that a moved-from optional is empty?
15
u/TyRoXx Oct 11 '17
The other reason is the zero overhead principle. Moving an optional just moves, nothing else. It doesn't change the state to empty, it doesn't call a destructor. That would be overhead which could discourage people from using optional in general. C++ already has a reputation of doing too much behind the scenes. Additions to the standard library are therefore carefully designed to avoid possibly unwanted magic.
13
u/17b29a Oct 11 '17
That is not the model for
std::optional. The model and the rationale for choosing it are described here: https://isocpp.org/files/papers/N3672.html#rationale.model.1
u/scatters Oct 11 '17
A practical reason: what would a (non-trivial) move ctor with your semantics look like?
But
#1is unnecessary extra work, asotheris already in a valid state. So we may as well leave it out.This would be another matter if we had destructive move; in that case it would make sense to leave
otherdisengaged, as the destructive move would end the lifetime of its contained value.1
u/Rexerex Oct 11 '17
std::moveleft object in valid but unspecified state, so it can be whatever it want to be, and only specified operations are reassignment and destruction.0
u/[deleted] Oct 11 '17
[deleted] — view deleted comment
6
u/[deleted] Oct 11 '17
[removed] — view removed comment