命名空间
变体
操作

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) 仅当 Sourcepath 不是相同类型,且满足下列条件之一时,才参与重载决议:

目录

[edit] 参数

p - 要追加的路径
str - 要追加的字符串或字符串视图
ptr - 指向要追加的空终止字符串开头的指针
x - 要追加的单个字符
source - std::basic_stringstd::basic_string_view、空终止多字符字符串,或指向空终止多字符序列的输入迭代器,它表示路径名(可移植或原生格式)
first, last - 一对 LegacyInputIterator,指定表示路径名的多字符序列
类型要求
-
InputIt 必须满足 LegacyInputIterator 的要求。
-
InputIt 的值类型必须是编码字符类型之一(charwchar_tchar16_tchar32_t)。
-
CharT 必须是编码字符类型之一(charwchar_tchar16_tchar32_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)
连接两条路径,带目录分隔符
(函数) [编辑]