std::filesystem::path::concat, std::filesystem::path::operator+=
来自 cppreference.cn
< cpp | filesystem | path
path& operator+=( const path& p ); |
(1) | (C++17 起) |
path& operator+=( const string_type& str ); path& operator+=( std::basic_string_view<value_type> str ); |
(2) | (C++17 起) |
path& operator+=( const value_type* ptr ); |
(3) | (C++17 起) |
path& operator+=( value_type x ); |
(4) | (C++17 起) |
template< class CharT > path& operator+=( CharT x ); |
(5) | (C++17 起) |
template< class Source > path& operator+=( const Source& source ); |
(6) | (C++17 起) |
template< class Source > path& concat( const Source& source ); |
(7) | (C++17 起) |
template< class InputIt > path& concat( InputIt first, InputIt last ); |
(8) | (C++17 起) |
连接当前路径与参数
1-3,6,7) 将 path(p).native() 追加到存储于 *this 中的路径名,以原生格式进行。这会直接操作 native() 的值,并且可能不具备跨操作系统移植性。
4,5) 等同于 return *this += std::basic_string_view(&x, 1);。
8) 等同于 return *this += path(first, last);。
(6) 与 (7) 仅当 Source
与 path
不是相同类型,且满足下列条件之一时,才参与重载决议:
-
Source
是 std::basic_string 或 std::basic_string_view 的特化,或 - std::iterator_traits<std::decay_t<Source>>::value_type 是合法的,且指代某个可能带 const 限定的编码字符类型(char、 char8_t、 (C++20 起)char16_t、 char32_t 或 wchar_t)。
目录 |
[edit] 参数
p | - | 要追加的路径 |
str | - | 要追加的字符串或字符串视图 |
ptr | - | 指向要追加的空终止字符串开头的指针 |
x | - | 要追加的单个字符 |
source | - | std::basic_string、 std::basic_string_view、空终止多字符字符串,或指向空终止多字符序列的输入迭代器,它表示路径名(可移植或原生格式) |
first, last | - | 一对 LegacyInputIterator,指定表示路径名的多字符序列 |
类型要求 | ||
-InputIt 必须满足 LegacyInputIterator 的要求。 | ||
-InputIt 的值类型必须是编码字符类型之一(char、 wchar_t、 char16_t 和 char32_t)。 | ||
-CharT 必须是编码字符类型之一(char、 wchar_t、 char16_t 和 char32_t)。 |
[edit] 返回值
*this
[edit] 异常
若内存分配失败,可能抛出 std::bad_alloc。
[edit] 注解
与 append() 或 operator/= 不同,此操作决不引入额外的目录分隔符。
[edit] 示例
运行此代码
#include <filesystem> #include <iostream> #include <string> int main() { std::filesystem::path p1; // an empty path p1 += "var"; // does not insert a separator std::cout << R"("" + "var" --> )" << p1 << '\n'; p1 += "lib"; // does not insert a separator std::cout << R"("var" + "lib" --> )" << p1 << '\n'; auto str = std::string{"1234567"}; p1.concat(std::begin(str) + 3, std::end(str) - 1); std::cout << "p1.concat --> " << p1 << '\n'; }
输出
"" + "var" --> "var" "var" + "lib" --> "varlib" p1.concat --> "varlib456"
[edit] 缺陷报告
下列行为更改缺陷报告被追溯应用到先前发布的 C++ 标准。
DR | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 3055 | C++17 | 单字符连接的规范形式不良 | 已良好成形 |
LWG 3244 | C++17 | 缺失 Source 不可以是 path 的约束 |
已添加 |
[edit] 参见
向路径追加带目录分隔符的元素 (公有成员函数) | |
(C++17) |
连接两条路径,带目录分隔符 (函数) |