Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将boost::visitor与unique_ptr的boost::变体一起使用_C++_C++11_Boost_Unique Ptr_Boost Variant - Fatal编程技术网

C++ 将boost::visitor与unique_ptr的boost::变体一起使用

C++ 将boost::visitor与unique_ptr的boost::变体一起使用,c++,c++11,boost,unique-ptr,boost-variant,C++,C++11,Boost,Unique Ptr,Boost Variant,我有两种类型的std::unique_ptr,它们保存在boost::variant中。我正在尝试编写boost::static_visitor的子类,以提取对底层对象的常量引用,这两个惟一的_ptr变体是我的boost::variant的模板。设置如下所示: using bitmap_flyweight_t = boost::flyweights::flyweight<allegro_bitmap_t>; using image_bitmap_flyweight_t = boost

我有两种类型的std::unique_ptr,它们保存在boost::variant中。我正在尝试编写boost::static_visitor的子类,以提取对底层对象的常量引用,这两个惟一的_ptr变体是我的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();
    }
};
使用位图_flyweight\u t=boost::flyweights::flyweight;
使用图像\u位图\u flyweight\u t=boost::flyweights::flyweight;
类位图\u访问者:public boost::static\u访问者
{
公众:
常量快板位图和运算符()(常量标准::唯一位图)常量
{
返回位图_ptr.get()->get();
}
常量快板位图和运算符()(常量标准::唯一位图)常量
{
返回位图_ptr.get()->get();
}
};

我能够使用移动语义在实例化时将唯一的_ptr放入对象的boost::variant成员变量中,没有编译器的抱怨。但是,当我尝试使用上述访问者访问变量类型时,编译器抱怨它无法访问,因为unique\u ptr不可复制构造。

接受对unique指针的常量引用

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();
    }
};

class bitmap\u访问者:public boost::static\u玩具项目的访问者。

你试过接受推荐吗?啊,当然。我对在我的项目中使用智能指针有点陌生,所以我认为传递“指针引用”的概念让我大吃一惊…@Bitrex只是把它们当作常规对象。无法复制不可复制的对象,仅此而已;)