Spyke

Posts

Combinatorial instantiation of C++ templates with std::variant

This one was quite a struggle, thought I'd share for the C++ programmers here.

I have a use case where for many function templates I have to instantiate them for a bunch of different parameter types, e.g. unary, binary and ternary functions where each argument can be one of 9 different types, giving 9, 9^2 = 81 or 9^3 = 729 instantiations of each function. Clearly I don't want to write those out as explicit template instantiations, and using macros is error prone too.

I've found this approach with std::variant and std::visit to be useful. Would appreciate any insight on edge cases where this may not work, or other suggested approaches.

Combinatorial instantiation of C++ templates with std::varianthttps://indii.org/blog/revisited-combinatorial-instantiation-of-templates-with-std-variant/Open linkView original on programming.dev

You reached the end