Does my code as is compile for anyone?
No it doesn't, variant will try to invoke the copy constructor, which is missing. (Foo::Foo(Foo const&)
isn't even declared):
boost/variant/variant.hpp|756 col 9| error: no matching function for call to ‘Foo::Foo(const Foo&)’
Even if you did declare it, it wouldn't work:
test.cpp|43 col 6| error: ‘Foo::Foo(const Foo&)’ is private
It has been mentioned in the comments but you need
to make the copy constructor a copy constructor (for good style)
Foo(Foo const& tt) = delete; Foo& operator=(const Foo& tt) = delete;
to make the move constructor/assignment
noexcept
, otherwise (likestd::vector
)variant
will refuse to move things because it wouldn't be exception safe.Foo(Foo && moveData) noexcept { } Foo& operator=(Foo && moveData) noexcept { return *this; }
Here's a simplified sample that compiles on GCC and Clang:
#include <iostream>
#include <memory>
#include <string>
#include <boost/variant.hpp>
struct UniqueTest { };
struct Foo
{
public:
std::unique_ptr<UniqueTest> testUniquePtr;
Foo() { std::cout << "Foo::Foo\n"; }
Foo(Foo && moveData) noexcept { }
Foo& operator=(Foo && moveData) noexcept { return *this; }
Foo(Foo const& tt) = delete;
Foo& operator=(const Foo& tt) = delete;
};
int main()
{
Foo x = Foo();
boost::variant<std::wstring, Foo> m_result2;
std::wstring testString = L"asdf";
m_result2 = testString; //Fails
//m_result2 = std::move(testString); //Fails
//m_result2 = std::move(x); //Fails
boost::get<Foo>(m_result2).testUniquePtr.get();
}