this post was submitted on 17 Aug 2023
8 points (100.0% liked)

C++

12 readers
1 users here now

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

Rules

founded 1 year ago
MODERATORS
 

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.

top 1 comments
sorted by: hot top controversial new old
[–] cschreib@programming.dev 2 points 1 year ago* (last edited 1 year ago)

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