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-qualified 的编码字符类型(char、char8_t、(C++20 起)char16_t、char32_t 或 wchar_t)。
目录 |
[编辑] 参数
p | - | 要附加的路径 |
str | - | 要附加的字符串或字符串视图 |
ptr | - | 指向要附加的以 null 结尾字符串开头的指针 |
x | - | 要附加的单个字符 |
source | - | std::basic_string、std::basic_string_view、以 null 结尾的多字符字符串,或指向以 null 结尾的多字符序列(表示路径名,可以是可移植格式或原生格式)的输入迭代器 |
first, last | - | 指定表示路径名的多字符序列的 LegacyInputIterator 对 |
类型要求 | ||
-InputIt 必须满足 LegacyInputIterator 的要求。 | ||
-InputIt 的值类型必须是编码字符类型之一(char、wchar_t、char16_t 和 char32_t)。 | ||
-CharT 必须是编码字符类型之一(char、wchar_t、char16_t 和 char32_t)。 |
[编辑] 返回值
*this
[编辑] 异常
如果内存分配失败,可能抛出 std::bad_alloc。
[编辑] 注意
与 append() 或 operator/= 不同,永远不会引入额外的目录分隔符。
[编辑] 示例
运行此代码
#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"
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 3055 | C++17 | 连接单个字符的规范格式不正确 | 使其格式正确 |
LWG 3244 | C++17 | 缺少 Source 不能是 path 的约束 |
已添加 |
[编辑] 另请参阅
用目录分隔符向路径追加元素 (public 成员函数) | |
(C++17) |
用目录分隔符连接两个路径 (函数) |