boost::variant; std::unique_ptr and copy

后端 未结 2 1155
温柔的废话
温柔的废话 2021-01-05 04:46

This Question Determined That a Non-Copyable Type Can\'t Be Used With Boost Variant

Tree class

template 

        
2条回答
  •  借酒劲吻你
    2021-01-05 05:10

    using Mixed = boost::variant< 
      std::unique_ptr,
      std::unique_ptr,
      std::unique_ptr,
      std::unique_ptr
    >;
    
    int main() {    
      auto md = std::unique_ptr(new int(123));
      Mixed mixed = std::move(md);
      std::cout << *boost::get< std::unique_ptr >(mixed) << std::endl;
      return 0;
    }
    

    unique_ptr is move-only and can be used in variant. The above example can compile and work (C++11).

提交回复
热议问题