2016-10-21 6 views
2

私はboost :: variantの内部に保持されているstd :: unique_ptrには2種類あります。 boost :: static_visitorのサブクラスを作成して、基礎となるオブジェクトへのconst参照を抽出しています.2つのunique_ptrバリアント、boost :: variantがテンプレート化されています。boost :: visitorにunique_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(); 
    } 
}; 

私は移動のセマンティクス、そこにはコンパイラの苦情を使用して、インスタンス上のオブジェクトのブースト::バリアントメンバ変数にunique_ptrsを置くことができるよ:セットアップは次のようになります。しかし、上記の訪問者を使用してバリアント型にアクセスしようとすると、コンパイラはunique_ptrがコピーコンストラクタブルではないため、できないと不平を言っています。

+3

参考にしてみましたか? – krzaq

答えて

2

一意のポインタへの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(); 
    } 
}; 

ここにおもちゃプロジェクトのlive demoがあります。

+0

ああ、確かに。私は私のプロジェクトでスマートなポインタを使用することに若干新しかったので、 "ポインタへの参照"を渡すという考えは私を引きつけていたと思います。 – Bitrex

+1

@Bitrexは普通のオブジェクトとして考える。コピー不可能なオブジェクトはコピーできません。それはそれです;) – krzaq

関連する問題