• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function
    <memory>

    std::unique_ptr::operator bool

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

    概要

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

    戻り値

    get() != nullptr
    

    #include <iostream>
    #include <memory>
    
    int main()
    {
      std::unique_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
    • ICC: ?
    • Visual C++: 2010 , 2012 , 2013
      • 2012までは、コンパイラがexplicit operator boolに対応していないため、不透明な型へのポインタ型への変換演算子関数として実装されている。

    参照