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

履歴 編集

function
<filesystem>

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

bool has_parent_path() const;

概要

パスに親パスが含まれているか判定する。

  • パスにルートパスのみが含まれていれば、ルートパスの親はルートパスと見なされてtrueが返る
  • ディレクトリパス (bar/) の場合、ディレクトリ内から見た親が自身のディレクトリ (bar) と見なされ、trueが返る
  • パスにファイル名、もしくはルート以外のディレクトリのみが含まれ、親パスの情報がない場合はfalseが返る

戻り値

POSIXベースシステムでの例

#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

int main()
{
  fs::path ps[] = {
    "/foo/bar.txt", // ファイル名を含むパス
    "/foo/bar/",    // ディレクトリパス
    "/",            // ルートパスのみ
    "bar/",         // ディレクトリパス
    "bar"           // ファイルパス
  };

  std::cout << std::boolalpha;
  for (const fs::path& p : ps) {
    std::cout << p << " : " << p.has_parent_path() << std::endl;
  }
}

出力

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

Windowsでの例

#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

int main()
{
  fs::path ps[] = {
    "C:/foo/bar.txt", // ファイル名を含むパス
    "C:/foo/bar/",    // ディレクトリパス
    "C:/",            // ルートパスのみ
    "bar/",         // ディレクトリパス
    "bar"           // ファイルパス
  };

  std::cout << std::boolalpha;
  for (const fs::path& p : ps) {
    std::cout << p << " : " << p.has_parent_path() << std::endl;
  }
}

出力

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

バージョン

言語

  • C++17

処理系