Skip to content

Instantly share code, notes, and snippets.

@ialex32x
Created April 17, 2025 10:28
Show Gist options
  • Save ialex32x/4420773590380f7ba54cb386fe34acfb to your computer and use it in GitHub Desktop.
Save ialex32x/4420773590380f7ba54cb386fe34acfb to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#define MAKE_FUNC_BODY(...) static __VA_ARGS__ { __assume(false); }
#define MAKE_TYPE_CONSTRAINT(Name, ...) struct Name { MAKE_FUNC_BODY(__VA_ARGS__) };
template<typename T>
int call(int index, T const& v) { std::cout << "-["<<index<<"] " << v << std::endl; return index+1; }
int call(int index, std::string const& v) { std::cout << "+["<<index<<"] " << v << std::endl; return 100; }
template<auto CommandT>
struct Dispatch;
template<
typename ReturnT,
typename... ArgsT,
ReturnT (*FunctionT)(ArgsT...)
>
struct Dispatch<FunctionT>
{
template<typename... ArgsT, int... Indices>
static auto dispatch(ArgsT const&... args, std::integer_sequence<int, Indices...>)
{
return (call(Indices, args) + ...);
}
static ReturnT with(ArgsT... args)
{
std::cout << "---" << std::endl;
return dispatch<ArgsT...>(args..., std::make_integer_sequence<int, sizeof...(ArgsT)>());
}
};
int main()
{
MAKE_TYPE_CONSTRAINT(
Test,
int call(int a, double b, const std::string& c, const std::string& d)
)
const int res = Dispatch<Test::call>::with(1, 2.0, "hello", "world");
std::cout<<"[RETURN] "<<res<<std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment