In general, the best way to do this is to take an object by value and let the caller decide whether to copy or move it:
template<typename T> optional<T> some(T x) { return optional<T>(std::move(x)); }
If the caller calls it temporary or uses std::move
by their value, it moves. Otherwise, it will be copied.
Yes, that means that you take one more step (which, if the movement is the same as copying, means making two copies). If a significant performance issue is important to you, you will have to use the implementation of two overloads.
source share