I think Boost::variant is busted in 1_54. I am trying to use a std::unique_ptr with as a bounded type in boost variant.
According to the 1_54 documentation, variant needs to be copy constructable or Move Constructable.
http://www.boost.org/doc/libs/1_54_0/doc/html/variant/reference.html
So I implemented the move constructors and disabled the copy constructors in my code.
When I try to assign something to the variant object it fails to compile. I have tried various different things including using std::move to assign data to the variant object, but nothing seems to work. Following the compilation error stack trace I determined the problem is in variant.hpp, where its trying to backup the rhs data. I would like to know what you guys think and let me know if I right to assume boost variant documentation is wrong.
Thanks in advance.
I am compiling with vs2010 and using C++11.
Here is my Test Code:
#include <iostream>
#include <memory>
#include <utility>
#include <vector>
#include <string>
#pragma warning (push)
#pragma warning (disable: 4127 4244 4265 4503 4512 4640 6011)
#include <boost/optional.hpp>
#include <boost/variant.hpp>
#include <boost/ref.hpp>
#include <boost/shared_ptr.hpp>
#pragma warning (pop)
#include <boost/foreach.hpp>
#include <boost/format.hpp>
using boost::format;
using boost::str;
using namespace std;
class UniqueTest
{
};
class Foo
{
public:
std::unique_ptr<UniqueTest> testUniquePtr;
Foo() { std::cout << "Foo::Foo\n"; }
Foo (Foo&& moveData)
{
}
Foo& operator=(Foo&& moveData)
{
return *this;
}
private:
Foo(Foo& tt);
Foo& operator=(const Foo& tt);
};
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 ();
return 0;
}
noexcept
(as the defaulted ones are implicitly).variant
will not use move ops if doing so could throw, to avoid data loss, in order to provide the strong exception safety guarantee – Excogitatenoexcept
– Dubbingnoexcept
, to see if that makesvariant
use it in preference to your private (and therefore unusable) copy constructor. That should be implicit if you don't define it manually, but VS2010 predates the C++11 standard and I have no idea how much of it is supported – Excogitatenoexcept
,=default
or quite a lot more. – Iconoclastnoexcept
– Excogitate<vector>
or<boost/optional.hpp>
or most of the other headers you include. – Excogitate