#include <ext/any_function>
Provides a std::function-compatible wrapper that can be called with a std::vector<std::any>. It is useful for command queues, reflection-like dispatch, and APIs that need to store callable objects while binding arguments later.
ext::any_function<R(Args...)>stores a callable and exposes both typed calls andstd::any-based calls.push_back,set_args, andclearmanage the stored argument vector used by the nullaryoperator()().call(args...)invokes the stored callable with typed arguments and returnsRdirectly.invalid_argument_typeandargument_count_mismatchdescribe failed dynamic argument conversion.
- Dynamic calls validate the number of arguments before invocation.
- Each
std::anyargument is cast to the function signature type; mismatches throw with the failing argument index. - Reference wrappers are supported for pass-by-reference command patterns.
- GCC 8.3.0+
- Clang 10.0+
- Visual Studio 2017+
- std::any required
- if constexpr required
#include <ext/any_function>
ext::any_function fn(strlen);
if (fn.call("test") == 4) {
}
if (std::any_cast<decltype(fn)::result_type>(fn({"test"})) == 4) {
}
size_t len;
std::any result;
result = fn({"test"});
if (result.has_value())
len = std::any_cast<size_t>(result);
std::vector<std::any> args;
args.push_back("test");
result = fn(args);
if (result.has_value())
len = std::any_cast<size_t>(result);