std::error_...operatorbool(3)

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

πŸ’– std::error_code::operatorbool πŸ’–

okay so this is the std::error_code::operatorbool page from the C++ Standard Libary manual, section (3) β€” yes bestie, that exact manual name, typo and all, no cap. πŸŽ€


🌸 NAME 🌸

std::error_code::operatorbool - std::error_code::operatorbool


✨ Synopsis ✨

the signature is literally this, and it’s been iconic since C++11:

explicit operator bool() const noexcept;  (since C++11)

Checks if the error code value is valid, i.e. non-zero.


🌈 Parameters 🌈

  • (none) β€” literally no parameters, not a single one, periodt. πŸ’…

πŸ”₯ Return value πŸ”₯

  • false if value() == 0
  • true otherwise

πŸ¦‹ Notes πŸ¦‹

okay so this is the part where we get real. Although this operator is often used as a convenient shorthand to check if any error was returned, as in:

if (ec) { /* handle error */ }

such use is not robust: some error codes, for example, HTTP status code 200, may indicate success as well. so like, vibe check before you assume error, okay? that’s the whole caveat, fr fr. 🫢


mwah πŸ’– source and date tea: this page is from http://cppreference.com, dated 2024.06.10, and it’s std::error_...operatorbool(3) β€” bye bestie, go check your error codes responsibly πŸ’…βœ¨

πŸ“¦ where this page comes from