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

履歴 編集

function
<filesystem>

std::filesystem::path::is_relative(C++17)

bool is_relative() const;

概要

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

戻り値

return !is_absolute();

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_relative() << std::endl;
  }
}

出力

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

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_relative() << std::endl;
  }
}

出力

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

バージョン

言語

  • C++17

処理系