I am making a class to store a reference to an object if given an lvalue in the constructor and to store a copy if given an rvalue. This is fine and compiles:
template <typename obj_t>
struct holder
{
obj_t obj;
template <typename obj_ref_t>
holder(obj_ref_t && o)
: obj(std::forward<obj_ref_t>(o))
{}
};
template <typename obj_t>
holder<obj_t> make_held(obj_t && o) {
return holder<obj_t>(std::forward<obj_t>(o));
}
However, when I add a destructor (even an empty one) to the class, I get a compiler error:
Invalid instantiation of non-const reference of type "obj_t &" from an rvalue of type "holder<obj_t &>"
Why does adding a destructor somehow invoke a constructor call to create a wrapped object out of an already wrapped object?