std::move_o...operatorbool(3)

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

Hey bestie! πŸ’– Welcome to std::move_o...operatorbool(3), folded in from the C++ Standard Libary manual (yes, that’s the manual name β€” we keep it exactly). This page is about std::move_only_function::operatorbool, and it’s lowkey the vibe check for whether your function is holding a callable target. ✨

✨ NAME ✨

  • std::move_only_function::operatorbool β€” yep, that’s the name, and the original repeats it: std::move_only_function::operatorbool. Iconic of it, honestly. πŸ’…

🌟 Synopsis 🌟

Here’s the exact signature, no edits, no cap:

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

It checks whether *this stores a callable target, i.e. is not empty. Basically: β€œdo I have a function in here or not?” πŸ‘€

πŸŽ€ Parameters πŸŽ€

None. Zero. Nada. This one takes no parameters. 🫢

πŸ’œ Return value πŸ’œ

  • true if *this stores a callable target
  • false otherwise

Periodt. βœ…

🌸 Example 🌸

This is the whole example, verbatim, because we do not gatekeep code:

// Run this code

#include <functional>
#include <iostream>

void sampleFunction()
{
    std::cout << "This is the sample function!\n";
}

void checkFunc(std::move_only_function<void() const> const& func)
{
    // Use operator bool to determine if callable target is available.
    if (func)
    {
        std::cout << "Function is not empty! Calling function.\n";
        func();
    }
    else
        std::cout << "Function is empty. Nothing to do.\n";
}

int main()
{
    std::move_only_function<void() const> f1{};
    std::move_only_function<void() const> f2{sampleFunction};

    std::cout << "f1: ";
    checkFunc(f1);

    std::cout << "f2: ";
    checkFunc(f2);
}

πŸ“ Output: πŸ“

f1: Function is empty. Nothing to do.
f2: Function is not empty! Calling function.
This is the sample function!

πŸ‘€ See also πŸ‘€

  • operator== β€” compares a std::move_only_function with nullptr (C++23) (function)
  • operator bool β€” checks if a target is contained (public member function of std::function<R(Args...)>)

Okay bestie, that’s the page! Source/version: http://cppreference.com; date: 2024.06.10. Stay slay, and may your callable targets never be empty. πŸ’–βœ¨

πŸ“¦ where this page comes from