std::filesy...::operator=(3)
β¨ std::filesystem::path::operator= β¨
Okay bestie, so this is the assignment operator for std::filesystem::path and it's literally the glow-up your path objects deserve. It's giving "replace everything inside me with a new pathname" energy and honestly? Iconic. Let's get into it π
πΈ SYNOPSIS πΈ
Like, here's the whole vibe:
path& operator=( const path& p ); (1) (since C++17)
path& operator=( path&& p ) noexcept; (2) (since C++17)
path& operator=( string_type&& source ); (3) (since C++17)
template< class Source > (4) (since C++17)
path& operator=( const Source& source );
(1) So this one replaces the contents of *this with a pathname where both the native and generic format representations equal those of p. No cap, both formats get the same treatment.
(2) Same deal, replaces the contents of *this with a pathname whose native and generic format representations equal those of p, but possibly using move semantics β meaning p is left in a valid but unspecified state. It's noexcept too, periodt.
(3) This one replaces the contents of *this with a new path value constructed from a detected-format source, and that source gets left in a valid but unspecified state too. Equivalent to assign(std::move(source)).
(4) Replaces the contents of *this with a new path value constructed from a detected-format source as if by overload (4) of the path constructor. Equivalent to assign(source).
Now, this overload (4) is a little picky about who it lets in β it participates in overload resolution only if Source and path are not the same type, and either:
Sourceis a specialization ofstd::basic_stringorstd::basic_string_view, orstd::iterator_traits<std::decay_t<Source>>::value_typeis valid and denotes a possibly const-qualified encoding character type (char,char8_t(since C++20),char16_t,char32_t, orwchar_t).
π PARAMETERS π
pβ a path to assignsourceβ astd::basic_string,std::basic_string_view, pointer to a null-terminated character/wide character string, or an input iterator that points to a null-terminated character/wide character sequence. The character type must be one ofchar,char8_t(since C++20),char16_t,char32_t,wchar_t
π¦ RETURN VALUE π¦
*this β that's it, that's the whole return value. She returns herself, main character energy fr fr.
π EXAMPLE π
Run this code bestie:
#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
}
π₯ DEFECT REPORTS π₯
The following behavior-changing defect reports were applied retroactively to previously published C++ standards:
- LWG 3244 β Applied to C++17. Behavior as published: the constraint that
Sourcecannot bepathwas missing. Correct behavior: it got added. Slay for standards compliance π
π SEE ALSO π
assignβ assigns contents (public member function)constructorβ constructs a path (public member function)
Okay so that's the whole operator, bestie. It's short but it's powerful and honestly it ate. Go forth and assign your paths with confidence π«Ά
Source: http://cppreference.com β 2024.06.10 β and yes, the manual name is std::filesy...::operator= in case you forgot (iykyk). Mwah, love u, bye! πβ¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms