Boost 1.53では、スマートポインタがnullptrに対応しました(チケット: #4116 (Support nullptr (C++0x))、コミット: Changeset 81780)。そこで、コミットを見れば明らかではありますが、確認の意味を込めて、何ができて何ができないのか自身でも試してみました。

初期化と等値比較演算子が使えるか試しました。本当は代入演算子と非等値演算子(!=)も試すべきなのですが省略しています。あと、ついでなのでBoost.SmartPtrライブラリの一部ではないunique_ptrも含めています(こちらは1.53以前からnullptr対応していたかもしれません)。

#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/shared_array.hpp>
#include <boost/smart_ptr/weak_ptr.hpp>
#include <boost/smart_ptr/scoped_ptr.hpp>
#include <boost/smart_ptr/scoped_array.hpp>
#include <boost/smart_ptr/intrusive_ptr.hpp>
 
#include <boost/interprocess/smart_ptr/unique_ptr.hpp>
#include <boost/checked_delete.hpp>
 
// コンパイルを通すためだけのダミー
struct hoge {};
void intrusive_ptr_add_ref(hoge*) {}
void intrusive_ptr_release(hoge*) {}
 
int main()
{
  using namespace boost;
  using boost::interprocess::unique_ptr;
 
  shared_ptr<int> shp = nullptr; shp == nullptr;
  // shared_array<int> sha = nullptr; sha == nullptr;
  shared_array<int> sha; sha == nullptr;
 
  //weak_ptr<int> wp = nullptr;
 
  //scoped_ptr<int> scp = nullptr; scp == nullptr;
  //scoped_array<int> sca = nullptr; sca == nullptr;
  scoped_ptr<int> scp; scp == nullptr;
  scoped_array<int> sca; sca == nullptr;
 
  intrusive_ptr<hoge> ip = nullptr; ip == nullptr;
 
  //unique_ptr<int, checked_deleter<int>> up = nullptr; up == nullptr;
  unique_ptr<int, checked_deleter<int>> up; up == nullptr;
}

コメントアウトしたものはコンパイルできない(実装されていない)ものです。すなわち、このような結果でした。

  • shared_ptrとintrusive_ptrはnullptrで初期化できる(shared_ptrのソースコード上は代入もできる)。比較演算もできる。
  • shared_array、scoped_ptr/scoped_arrayそしてunique_ptrは、比較演算のみできる。

スポンサード リンク

この記事のカテゴリ

  • ⇒ Boost 1.53ではスマートポインタがnullptrに対応した