mercredi 13 septembre 2023

Is there anyway to restrict c++ template class parameters within one-to-one relationship?

For example, there is a template class:

template<typename A, typename B>
class Example;

If a pair of (A, B) is specialized, then other types cannot specialized with A. For example, (B, C) is allowed, but (A, C) is not allowed.

Example<int, double> E1, E2, E3;  // OK
Example<int, double> E4;          // OK
Example<int, bool> E5;            // Not Allowed (Because <int, double> already exists)
Example<bool, double> E6;         // OK
Example<double, double> E7;       // OK
Example<double, int> E8;          // Not Allowed (<double, double> already exists)

I try to use a std::map between template parameter A and B during runtime, but if there is a way to do this during compiling? Like SFINAE or other techniques?

Aucun commentaire:

Enregistrer un commentaire