我认为Boost::variant在1_54中被破坏。
我正在尝试使用std::unique_ptr作为boost变体中的有界类型。

根据1_54文档,变体需要可复制构造或可移动构造。

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

因此,我在代码中实现了move构造函数并禁用了复制构造函数。

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

提前致谢。

我正在使用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;
}

最佳答案



不,不是,variant将尝试调用缺少的拷贝构造函数。甚至没有声明(Foo::Foo(Foo const&)):

boost/variant/variant.hpp|756 col 9| error: no matching function for call to ‘Foo::Foo(const Foo&)’

即使您确实声明了它,也行不通:
test.cpp|43 col 6| error: ‘Foo::Foo(const Foo&)’ is private

它已在评论中提及,但您需要
  • 使拷贝构造函数成为拷贝构造函数(用于良好样式)
    Foo(Foo const& tt) = delete;
    Foo& operator=(const Foo& tt) = delete;
    
  • 来创建移动构造函数/分配noexcept,否则(像std::vector一样)variant将拒绝移动内容,因为它不是异常安全的。
    Foo(Foo && moveData) noexcept { }
    Foo& operator=(Foo && moveData) noexcept { return *this; }
    

  • 这是在GCC和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();
    }
    

    关于c++ - boost 变体在1_54中被破坏?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19718726/

    10-11 22:49
    查看更多