- filesystem[meta header]
- std::filesystem[meta namespace]
- path[meta class]
- function[meta id-type]
- cpp17[meta cpp]
path parent_path() const;
親のパスを取得する。
パスにルートパスのみが含まれている場合は、ルートパスがそのまま返る。ルートパスの親は自身のパスであると見なされる。
!
has_relative_path()
であれば*this
が返る。そうでなければ、汎用フォーマットのパスに対して、ディレクトリ区切りした要素のうち、末尾要素を除いたパスを返す。
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
fs::path ps[] = {
"/foo/bar.txt", // ファイル名を含むパス
"/foo/bar/", // ディレクトリパス
"/foo/bar", // ディレクトリパス(末尾/なし)
"/" // ルートパスのみ (ルートパスの親はルートパスなのでそのまま返る)
};
for (const fs::path& p : ps) {
std::cout << p << " : " << p.parent_path() << std::endl;
}
}
- parent_path()[color ff0000]
"/foo/bar.txt" : "/foo"
"/foo/bar/" : "/foo/bar"
"/foo/bar" : "/foo"
"/" : "/"
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
fs::path ps[] = {
"C:/foo/bar.txt", // ファイル名を含むパス
"C:/foo/bar/", // ディレクトリパス
"C:/" // ルートパスのみ (ルートパスの親はルートパスなのでそのまま返る)
};
for (const fs::path& p : ps) {
std::cout << p << " : " << p.parent_path() << std::endl;
}
}
- parent_path()[color ff0000]
"C:/foo/bar.txt" : "C:/foo"
"C:/foo/bar/" : "C:/foo/bar"
"C:/" : "C:/"
- C++17
- Clang:
- GCC: 8.1
- Visual C++: 2017 Update 7
- P0488R0 WG21 Working Paper, NB Comments, ISO/IEC CD 14882
- US 58のNBコメントによって、ルートパスの親パスを取得しようとした場合に
*this
が返るようになった
- US 58のNBコメントによって、ルートパスの親パスを取得しようとした場合に
- P0492R2 Proposed Resolution of C++17 National Body Comments for Filesystems(R2)