Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
Created February 8, 2026 01:48
Show Gist options
  • Select an option

  • Save dgodfrey206/33a664a28da3f16bf5831ff60ebf91e9 to your computer and use it in GitHub Desktop.

Select an option

Save dgodfrey206/33a664a28da3f16bf5831ff60ebf91e9 to your computer and use it in GitHub Desktop.
#include<vector>
#include<functional>
#include <bits/stdc++.h>
using namespace std;
enum ScalarType {
UINT8,
Float,
Byte,
};
struct Container {
void* data;
ScalarType scalar_type;
};
template<class T>
void kernel(T* data, int some_arg, int some_other_arg) {
// Do something
}
struct kernel_callable {
template<class... Ts>
void operator()(Ts&&... ts) const {
return kernel( std::forward<Ts>(ts)... );
}
};
// Bind a container arg1 as the type pointer
template<class func>
struct dispatch
{
template<size_t I> struct scalar_type_at;
template<> struct scalar_type_at<0> : std::type_identity<std::uint8_t> {};
template<> struct scalar_type_at<1> : std::type_identity<float> {};
template<> struct scalar_type_at<2> : std::type_identity<std::byte> {};
template<size_t... Is, class... Args>
static void dispatch_helper( std::index_sequence<Is...>, Container& c, Args&&... args) {
using F = void(Container&, Args&&...);
static F* const table[] = {
[](Container& c, Args&&... args) {
using E = typename scalar_type_at<Is>::type;
return func{}( static_cast<E*>(c.data), std::forward<Args>(args)... );
}...
};
table[static_cast<unsigned>(c.scalar_type)](c, std::forward<Args>(args)...);
}
template<class... Args>
inline void operator()(Container& c, Args... args)
{
dispatch_helper( std::make_index_sequence<3>{}, c, std::forward<Args>(args)... );
}
};
int main() {
std::vector<float> storage = {0., 1., 2., 3.};
Container container = {storage.data(), ScalarType::Float};
dispatch<kernel_callable>{}(container, 37, 51);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment