使用boost::visitor和unique_ptr的boost::变体
Using boost::visitor with a boost::variant of unique_ptr
我有两种std::unique_ptr类型,它们保存在boost::变量中。我正试图编写boost::static_visitor的子类,以提取对底层对象的两个unique_ptr变体的const引用,我的boost::variant是模板化的。设置看起来像这样:
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();
}
};
我可以在实例化时使用move语义将unique_ptrs放入对象的boost::variant成员变量中,没有编译器抱怨。然而,当我尝试使用上述访问器访问变量类型时,编译器抱怨它不能这样做,因为unique_ptr不是可复制构造的。
接受唯一指针的const引用。
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();
}
};
这是一个玩具项目的实时演示。
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- boost::进程间消息队列引发错误
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- cmake如何在fedora工作站中找到boost静态库包
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- Boost Graph Library,修复节点大小
- 什么是"#include <boost/functional/hash.hpp> "?
- 基于boost的程序的静态链接——zlib问题
- C++:如何在CLion IDE中安装Boost
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何在boost beast http请求中设置http头
- Boost Spirit,获取迭代器内部语义动作
- boost::shared_ptr和std::shared-ptr的同居
- 我如何在void*和boost共享ptr之间切换
- 数据突然从 Boost shared_array/ptr 中删除
- 我可以用std::shared_ptr而不是boost::shared-ptr构建boost库吗
- 使用boost lambda构造创建初始化的智能ptr的容器
- Boost Scoped Ptr-声明与分配
- 如何转发声明boost智能ptr
- boost ptr容器与普通容器的区别