Created
April 17, 2025 10:28
-
-
Save ialex32x/4420773590380f7ba54cb386fe34acfb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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