【发布时间】:2016-10-21 18:47:06
【问题描述】:
我有两种类型的 std::unique_ptr,它们保存在 boost::variant 中。我正在尝试编写 boost::static_visitor 的子类,以提取对我的 boost::variant 模板化的两个 unique_ptr 变体的底层对象的 const 引用。设置如下所示:
using bitmap_flyweight_t = boost::flyweights::flyweight<allegro_bitmap_t>;
using image_bitmap_flyweight_t = boost::flyweights::flyweight<boost::flyweights::key_value<const char*, allegro_bitmap_t>>;
class bitmap_visitor : public boost::static_visitor<allegro_bitmap_t>
{
public:
const allegro_bitmap_t& operator()(const std::unique_ptr<bitmap_flyweight_t> bitmap_ptr) const
{
return bitmap_ptr.get()->get();
}
const allegro_bitmap_t& operator()(const std::unique_ptr<image_bitmap_flyweight_t> bitmap_ptr) const
{
return bitmap_ptr.get()->get();
}
};
我能够在使用移动语义进行实例化时将 unique_ptrs 放入对象的 boost::variant 成员变量中,那里没有编译器投诉。但是,当我尝试使用上述访问者访问变体类型时,编译器抱怨它不能这样做,因为 unique_ptr 不是可复制构造的。
【问题讨论】:
-
您是否尝试过接受参考?
标签: c++ c++11 boost unique-ptr boost-variant