- filesystem[meta header]
- std::filesystem[meta namespace]
- path[meta class]
- function[meta id-type]
- cpp17[meta cpp]
bool has_parent_path() const;パスに親パスが含まれているか判定する。
- パスにルートパスのみが含まれていれば、ルートパスの親はルートパスと見なされて
trueが返る - ディレクトリパス (
bar/) の場合、ディレクトリ内から見た親が自身のディレクトリ (bar) と見なされ、trueが返る - パスにファイル名、もしくはルート以外のディレクトリのみが含まれ、親パスの情報がない場合は
falseが返る
return !parent_path().empty();- parent_path()[link parent_path.md]
- empty()[link empty.md]
#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;
}
}- has_parent_path()[color ff0000]
"/foo/bar.txt" : true
"/foo/bar/" : true
"/" : true
"bar/" : true
"bar" : false
#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;
}
}- has_parent_path()[color ff0000]
"C:\foo\bar.txt" : true
"C:\foo\bar\" : true
"C:\" : true
"bar\" : true
"bar" : false
Windowsでの例は、Visual C++が正式にファイルシステムライブラリをサポートしていないことから、未検証のサンプルコード・出力となっている。
- C++17
- Clang:
- GCC, C++17 mode: 8.1
- Visual C++: