#include <kumi/kumi.hpp>
#include <iostream>
#include <memory>
#include <functional>
#include <string>
int add(int a, int b) { return a + b; }
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"; }
};
struct MyClass
{
int value = 42;
int get()
const {
return value; }
void set(int v) { value = v; }
};
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): ";
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 << "\n===== invoke_r<void> =====\n";
std::cout << "invoke_r<void> works correctly\n";
return 0;
}
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
decltype(auto) constexpr get(array< T, N > &t) noexcept
Extracts the Ith element from a kumi::array.
Definition array.hpp:672