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

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

πŸ’– NAME πŸ’–

std::system_error::operator= β€” it's literally the assignment operator for std::system_error, bestie. Copy-paste energy but make it C++.

So this page is from the C++ Standard Library manual, section 3, and the page itself is std::system...::operator=(3). Yes the title is truncated with ... and that's just how cppreference rolls, no cap.


✨ Synopsis ✨

system_error& operator=( const system_error& other ) noexcept;

<span style="color:cyan">(since C++11)</span> β€” so she's been iconic since C++11, we love a consistent queen πŸ‘‘

What she does: assigns the contents of other into *this. Basically a hand-me-down but make it official.

And here's the ✨fancy✨ part: if both *this and other have dynamic type std::system_error, then after the assignment you get:

std::strcmp(what(), other.what()) == 0

Like, the what() strings match exactly. Vibe check passed βœ…


πŸŽ€ Parameters πŸŽ€

  • other β€” another system_error object to assign with. That's it. That's the whole parameter. She's low maintenance πŸ’…

🌸 Return value 🌸

*this β€” she returns herself, main character energy fr fr πŸ‘‘


πŸ¦‹ Example πŸ¦‹

okay so run this code, it's giving demonstration:

// Run this code

#include <cassert>
#include <cstring>
#include <iostream>
#include <system_error>

void print(const std::system_error& e)
{
    std::cout << "code:    [" << e.code() << "]\n"
                 "message: [" << e.code().message() << "]\n"
                 "what:    [" << e.what() << "]\n\n";
}

int main()
{
    std::system_error e1(EDOM, std::generic_category(), "Error info #1");
    print(e1);

    std::system_error e2(EIO, std::system_category(), "Error info #2");
    print(e2);

    e1 = e2;
    assert(std::strcmp(e1.what(), e2.what()) == 0);
    print(e1);
}

Notice that assert? That's the receipts for the whole strcmp(what(), other.what()) == 0 thing. She said prove it, and then she proved it 😭


🌈 Possible output 🌈

code:    [generic:33]
message: [Numerical argument out of domain]
what:    [Error info #1: Numerical argument out of domain]

code:    [system:5]
message: [Input/output error]
what:    [Error info #2: Input/output error]

code:    [system:5]
message: [Input/output error]
what:    [Error info #2: Input/output error]

See how the third block is a total clone of the second? That's the assignment doing its thing β€” e1 fully absorbed e2's whole personality. Slay πŸ’€

heads up bestie: the page says Possible output, so don't come for me if your terminal prints it slightly differently. It said possible, not guaranteed πŸ™ˆ

sign-off time 🫢 β€” this lil masterpiece lives over at http://cppreference.com, dated 2024.06.10, and it's page std::system...::operator=(3) from the C++ Standard Library manual. Bye bestie, go assign something πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from