KUMI v5.0.0
Gorgeous Garnet
Loading...
Searching...
No Matches
kumi::invoke_r

Invoke the Callable object c with a pack of arguments with return type R. More...

Variables

template<typename R>
constexpr invoke_r_t< R > kumi::invoke_r {}
 Callable object invoking the Callable object c with a pack of arguments and returning a value of type R.

Detailed Description

Invoke the Callable object c with a pack of arguments with return type R.

Variable Documentation

◆ invoke_r

template<typename R>
invoke_r_t<R> kumi::invoke_r {}
inlineconstexprnoexcept

Callable object invoking the Callable object c with a pack of arguments and returning a value of type R.

kumi::invoke_r is a replacement for std::invoke_r that avoids depending on <functional>. It invokes c but returns a value of type R rather than the actual return type of c. If R is void, the return value of c is discarded.

Note
This function does not take part in overload resolution if c can't be invoked with the given arguments or if the result can't be converted to R.

Header file

#include <kumi/functional/invoke.hpp>

Call Signature

template<typename R, typename C, typename... Ts>
constexpr R invoke_r(C && c, Ts && ... ts) noexcept
constexpr invoke_r_t< R > invoke_r
Callable object invoking the Callable object c with a pack of arguments and returning a value of type...
Definition invoke.hpp:173

Parameters

  • R: The type of the returned value
  • c: Callable object to be invoked
  • ts: Arguments to be passed to c

Return value

  • The value returned by c, converted to type R.

Helper type

template<typename R, typename C, typename... Ts>
using invoke_r_t = decltype(kumi::invoke_r<R>(kumi::declval<C>(), kumi::declval<Ts>()...));
template<typename R, typename C, typename... Ts> struct invoke_r
{
using type = kumi::result::invoke_r_t<R, C, Ts...>;
};

Computes the return type of a call to kumi::invoke_r

Example

#include <kumi/kumi.hpp>
#include <iostream>
#include <memory>
#include <functional>
#include <string>
// --- Free function ---
int add(int a, int b) { return a + b; }
// --- Callable objects ---
struct Adder
{
int operator()(int a, int b) const { return a + b; }
};
struct Multiplier
{
int operator()(int a, int b) const { return a * b; }
};
struct Greeter
{
std::string operator()(std::string const& name) const { return "Hello, " + name + "!"; }
};
struct VoidCallable
{
void operator()(int x) const { std::cout << "Value: " << x << "\n"; }
};
// --- Member types ---
struct MyClass
{
int value = 42;
int get() const { return value; }
void set(int v) { value = v; }
};
// --- invoke_r helpers ---
struct FloatGetter
{
constexpr float get() const noexcept { return 42.0; }
};
struct VoidGetter
{
constexpr void get() const noexcept {}
};
void void_func() { std::cout << "void_func called\n"; }
int main()
{
std::cout << "===== invoke free function =====\n";
std::cout << "invoke(add, 3, 4) = " << kumi::invoke(add, 3, 4) << "\n\n";
std::cout << "===== invoke callable objects =====\n";
std::cout << "Adder(3, 4): " << kumi::invoke(Adder{}, 3, 4) << "\n";
std::cout << "Multiplier(3, 4): " << kumi::invoke(Multiplier{}, 3, 4) << "\n";
std::cout << "Greeter(\"World\"): " << kumi::invoke(Greeter{}, "World") << "\n";
std::cout << "VoidCallable(42): ";
kumi::invoke(VoidCallable{}, 42);
std::cout << "\n===== invoke member function pointer =====\n";
MyClass obj{100};
std::cout << "obj.get() via invoke: " << kumi::invoke(&MyClass::get, obj) << "\n";
std::cout << "\n===== invoke member object pointer =====\n";
std::cout << "obj.value via invoke: " << kumi::invoke(&MyClass::value, obj) << "\n";
std::cout << "\n===== invoke with unique_ptr =====\n";
auto ptr = std::make_unique<MyClass>(200);
std::cout << "ptr->value via invoke: " << kumi::invoke(&MyClass::value, ptr) << "\n";
std::cout << "\n===== invoke with shared_ptr =====\n";
auto shared = std::make_shared<MyClass>(300);
std::cout << "shared->value via invoke: " << kumi::invoke(&MyClass::value, shared) << "\n";
std::cout << "\n===== invoke with reference_wrapper =====\n";
MyClass obj3{42};
std::reference_wrapper<MyClass> ref{obj3};
std::cout << "ref.get() via invoke: " << kumi::invoke(&MyClass::get, ref) << "\n";
std::cout << "\n===== invoke_r with type conversion =====\n";
constexpr auto getter = FloatGetter{};
std::cout << "invoke_r<int>(3.14f) = " << kumi::invoke_r<double>(&FloatGetter::get, getter) << "\n";
std::cout << "invoke_r<double>(42) = " << kumi::invoke_r<std::size_t>(&add, 1, 2) << "\n";
std::cout << "\n===== invoke_r<void> =====\n";
kumi::invoke_r<void>(&void_func);
kumi::invoke_r<void>(&VoidGetter::get, VoidGetter{});
std::cout << "invoke_r<void> works correctly\n";
return 0;
}
constexpr invoke_t invoke
Callable object invoking the Callable object c with a pack of arguments.
Definition invoke.hpp:105
decltype(auto) constexpr get(array< T, N > &t) noexcept
Extracts the Ith element from a kumi::array.
Definition array.hpp:672