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.

2 points
*

Neat! First real world use case I see for std::visit taking more than one variant.

permalink
report
reply

C++

!cpp@programming.dev

Create post

The center for all discussion and news regarding C++.

Rules

  • Respect instance rules.
  • Don’t be a jerk.
  • Please keep all posts related to C++.

Community stats

  • 1

    Monthly active users

  • 125

    Posts

  • 79

    Comments