std::filesy...::operator=(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

✨ 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:

  • Source is a specialization of std::basic_string or std::basic_string_view, or
  • std::iterator_traits<std::decay_t<Source>>::value_type is valid and denotes a possibly const-qualified encoding character type (char, char8_t (since C++20), char16_t, char32_t, or wchar_t).

πŸŽ€ PARAMETERS πŸŽ€

  • p β€” a path to assign
  • source β€” a std::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 of char, 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 Source cannot be path was 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