std::move_o...operatorbool(3)
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 π
trueif*thisstores a callable targetfalseotherwise
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 astd::move_only_functionwithnullptr(C++23) (function)operator boolβ checks if a target is contained (public member function ofstd::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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms