std::filesystem::path::operator=
来自 cppreference.cn
< cpp | filesystem | path
path& operator=( const path& p ); |
(1) | (C++17 起) |
path& operator=( path&& p ) noexcept; |
(2) | (C++17 起) |
path& operator=( string_type&& source ); |
(3) | (C++17 起) |
template< class Source > path& operator=( const Source& source ); |
(4) | (C++17 起) |
1) 用路径名替换 *this 的内容,该路径名的原生和通用格式表示都等于 p 的表示。
2) 用路径名替换 *this 的内容,该路径名的原生和通用格式表示都等于 p 的表示,可能使用移动语义:p 留在有效但未指定的状态。
3) 用从检测格式的 source 构造的新路径值替换 *this 的内容,source 留在有效但未指定的状态。等效于 assign(std::move(source))。
(4) 仅当 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)。
内容 |
[编辑] 参数
p | - | 要赋值的路径 |
source | - | std::basic_string、std::basic_string_view、指向空终止字符/宽字符字符串的指针,或指向空终止字符/宽字符序列的输入迭代器。字符类型必须是 char、char8_t、(C++20 起)char16_t、char32_t、wchar_t 之一 |
[编辑] 返回值
*this
[编辑] 示例
运行此代码
#include <filesystem> namespace fs = std::filesystem; int main() { fs::path p = "C:/users/abcdef/AppData/Local"; p = p / "Temp"; // move assignment const wchar_t* wstr = L"D:/猫.txt"; p = wstr; // assignment from a source }
[编辑] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 3244 | C++17 | 缺少 Source 不能是 path 的约束 |
已添加 |
[编辑] 参见
赋值内容 (公共成员函数) | |
构造 path (公共成员函数) |