• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    function
    <filesystem>

    std::filesystem::path::is_absolute

    bool is_absolute() const;
    

    概要

    パスが絶対パスかを判定する。

    絶対パスとは、追加の開始位置を必要とせずに、曖昧さなく特定の場所の場所を参照できるパスのことである。その定義はOSに依存する。

    戻り値

    パスが絶対パスであればtrue、そうでなければfalseを返す。

    備考

    • POSIXとWindowsはどちらも、ルートディレクトリがパスに含まれていれば、絶対パスと見なせる。カレントディレクトリや親ディレクトリの参照が含んでいても絶対パスと判断される

    POSIXベースシステムでの例

    #include <iostream>
    #include <filesystem>
    
    namespace fs = std::filesystem;
    
    int main()
    {
      fs::path ps[] = {
        "/",               // ルートパスのみ
        "/foo/bar.txt",    // ルートパスを含む
        "/foo/../bar.txt", // ルートパスに加えて、親ディレクトリの参照を含む
        "foo/bar.txt"      // ルートパスを含まない
      };
    
      std::cout << std::boolalpha;
      for (const fs::path& p : ps) {
        std::cout << p << " : " << p.is_absolute() << std::endl;
      }
    }
    

    出力

    "/" : true
    "/foo/bar.txt" : true
    "/foo/../bar.txt" : true
    "foo/bar.txt" : false
    

    Windowsでの例

    #include <iostream>
    #include <filesystem>
    
    namespace fs = std::filesystem;
    
    int main()
    {
      fs::path ps[] = {
        "C:",                // ルート名のみ
        "C:/",               // ルートパスのみ
        "C:/foo/bar.txt",    // ルートパスを含む
        "C:/foo/../bar.txt", // ルートパスに加えて、親ディレクトリの参照を含む
        "foo/bar.txt",       // ルートパスを含まない
        "C:foo",             // ルート名はあるがルートディレクトリはない
        "/foo"               // ルートディレクトリはあるがルート名はない
      };
    
      std::cout << std::boolalpha;
      for (const fs::path& p : ps) {
        std::cout << p << " : " << p.is_absolute() << std::endl;
      }
    }
    

    出力

    "C:" : false
    "C:/" : true
    "C:/foo/bar.txt" : true
    "C:/foo/../bar.txt" : true
    "foo/bar.txt" : false
    "C:foo" : false
    "/foo" : false
    

    バージョン

    言語

    • C++17

    処理系

    参照