std::functi...operatorbool(3)

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

πŸ‘‘ NAME πŸ‘‘

okay so welcome to the std::functi...operatorbool(3) page from the C++ Standard Libary (yes, that's the header's spelling 😭), aka the C++ Standard Library. This is std::function::operatorbool β€” and yep, that's the whole name again: std::function::operatorbool. It's the vibe check operator for whether your std::function is holding a callable function target or is just sitting there empty like an unread group chat πŸ’€.


✨ Synopsis ✨

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

Checks whether *this stores a callable function target, i.e. is not empty. That's the whole vibe check, bestie: callable target present = not empty; no callable target = empty. Periodt.


πŸ’œ Parameters πŸ’œ

(none) β€” literally none, bestie. Not a single parameter. This operator needs zero help πŸ’….


πŸ“ Return value πŸ“

true if *this stores a callable function target, false otherwise. No cap, that's it.


🌈 Example 🌈

// Run this code β€” yes, actually run it, it's cute. Here's the whole demo:

#include <functional>
#include <iostream>

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

void checkFunc(std::function<void()> 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::function<void()> f1;
    std::function<void()> 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 bool checks if the std::move_only_function has a target (C++23) β€” public member function of std::move_only_function.

Source/version/date check: this page is from http://cppreference.com, dated 2024.06.10, and filed as std::functi...operatorbool(3). Bye bestie, go give your callables a vibe check πŸ«Άβœ¨πŸ’…

πŸ“¦ where this page comes from