最終更新日時:
が更新

履歴 編集

function
<memory>

std::shared_ptr::operator bool(C++11)

explicit
  operator bool() const noexcept; // (1) C++11
constexpr explicit
  operator bool() const noexcept; // (1) C++26

概要

有効なリソースを所有しているかを判定する。

戻り値

get() != nullptr

例

#include <iostream>
#include <memory>

int main()
{
  std::shared_ptr<int> p(new int(3));

  if (p) {
    std::cout << "p has resource" << std::endl;
  }
  else {
    std::cout << "p doesn't have resource" << std::endl;
  }
}

出力

p has resource

バージョン

言語

  • C++11

処理系

  • GCC: 4.4.7 ✅
  • Clang: 3.0 ✅
  • Visual C++: 2008 (TR1) ✅, 2010 ✅, 2012 ✅, 2013 ✅
    • 2012までは、コンパイラがexplicit operator boolに対応していないため、不透明な型へのポインタ型への変換演算子関数として実装されている。

参照