提升变量在1_54中被破坏?

luk*_*gnh 6 c++ boost variant c++11

我认为Boost :: variant在1_54中被破坏了.我试图在boost变量中使用std :: unique_ptr作为有界类型.

根据1_54文档,变体需要是可复制的或Move Constructable.

http://www.boost.org/doc/libs/1_54_0/doc/html/variant/reference.html

所以我实现了移动构造函数并在我的代码中禁用了复制构造函数.

当我尝试将某些内容分配给变体对象时,它无法编译.我尝试了各种不同的东西,包括使用std :: move为变量对象分配数据,但似乎没有任何效果.在编译错误堆栈跟踪之后我确定问题出在variant.hpp中,它试图备份rhs数据.我想知道你们的想法,让我知道我是否正确地认为提升变体文档是错误的.

提前致谢.

我正在使用vs2010编译并使用C++ 11.

这是我的测试代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

seh*_*ehe 4

\n

我的代码可以为任何人编译吗?

\n
\n\n

不,它不会,变体将尝试调用缺少的复制构造函数。(Foo::Foo(Foo const&)甚至没有声明):

\n\n
boost/variant/variant.hpp|756 col 9| error: no matching function for call to \xe2\x80\x98Foo::Foo(const Foo&)\xe2\x80\x99\n
Run Code Online (Sandbox Code Playgroud)\n\n

即使你确实声明了它,它也不会起作用:

\n\n
test.cpp|43 col 6| error: \xe2\x80\x98Foo::Foo(const Foo&)\xe2\x80\x99 is private\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

评论中已提到,但您需要

\n\n
    \n
  • 使复制构造函数成为复制构造函数(为了良好的风格)

    \n\n
    Foo(Foo const& tt) = delete;\nFoo& operator=(const Foo& tt) = delete;\n
    Run Code Online (Sandbox Code Playgroud)
  • \n
  • 进行移动构造函数/赋值noexcept,否则(如std::vectorvariant将拒绝移动事物,因为它不是异常安全的。

    \n\n
    Foo(Foo && moveData) noexcept { }    \nFoo& operator=(Foo && moveData) noexcept { return *this; }\n
    Run Code Online (Sandbox Code Playgroud)
  • \n
\n\n

这是在 GCC 和 Clang 上编译的简化示例:

\n\n
#include <iostream>\n#include <memory>\n#include <string>\n#include <boost/variant.hpp>\n\nstruct UniqueTest { };\n\nstruct Foo\n{\npublic:\n    std::unique_ptr<UniqueTest> testUniquePtr;\n\n    Foo() { std::cout << "Foo::Foo\\n"; }\n    Foo(Foo && moveData) noexcept { }\n\n    Foo& operator=(Foo && moveData) noexcept { return *this; }\n\n    Foo(Foo const& tt) = delete;\n    Foo& operator=(const Foo& tt) = delete;\n};\n\n\nint main()\n{\n    Foo x = Foo();\n\n    boost::variant<std::wstring, Foo> m_result2;\n\n    std::wstring  testString = L"asdf";\n    m_result2 = testString; //Fails\n    //m_result2 = std::move(testString); //Fails\n    //m_result2 = std::move(x); //Fails\n    boost::get<Foo>(m_result2).testUniquePtr.get();\n}\n
Run Code Online (Sandbox Code Playgroud)\n