I have a resource-wrapper class which is noncopyable, but is movable. Something like this (pseudeocode)
class Wrapper {
SomeResource* m_handle = nullptr;
public:
Wrapper(const Wrapper&) = delete;
Wrapper& operator=(const Wrapper&) = delete;
Wrapper(SomeResource* handle) : m_handle(handle) { }
Wrapper(Wrapper&& other) {
std::swap(m_handle, other.m_handle);
}
}
This is all well and good, however I have a number of these, and I have a function which parses some data and returns EITHER a Wrapper or an alternative wrapper. I thought of using std::variant
to express this in a return value. E.g.
std::variant<Wrapper, AlternativeWrapper> LoadData(void* bytes, size_t len)
{ ... }
I can write this function, and it all compiles. I.e. I construct a Wrapper inside the LoadData function, then I can move it into the variant which is then returned.
But on the other side, when I want to get the value out, get this error (MSVC2019)
error C2280: 'Wrapper::Wrapper(const Wrapper&)': attempting to reference a deleted function
My code looks like this.
auto result = LoadData(bytes, len);
std::get<Wrapper>(result);
That makes sense as the result is still sticking around, but then how do I access it?
std::get<Wrapper>(result);
further? – Transponder