c++ - Symmetric template's arguments -


suppose have converting routine class. if can convert t class u, automatically can convert vise versa.

i represent template class , specializations:

template <typename t, typename u> class convert;  template <> class convert<a,b> {     static int param() { return 42; } }  template <> class convert<b,a> {     static int param() { return -convert<a,b>::param(); } } 

this works good, when need add new type routine, must add 2 specializiations. can reduce number 1 defining general reverse template class this:

template <typename t, typename u> class convert {     static int param() { return -convert<u,t>::param(); } } 

which work if have convert specialization?

thanks in advance.

here suggestion in comment elaborated:

#include<iostream> #include<type_traits>  struct a{}; struct b{}; struct c{};  template <typename ... args> struct convert;  template <typename t> struct convert<t,t> {     static int param() { return 0; } };  template <typename t, typename u> struct convert<t,u> {     static decltype(-convert<u,t>::param()) param() { return -convert<u,t>::param(); } };  template <> struct convert<a,b> {     static int param() { return 42; } };  template <> struct convert<a,c> {     static int param() { return 43; } };  template <> struct convert<b,c> {     static int param() { return 44; } };  int main() {     std::cout<<convert<a,b>::param()<<std::endl;     std::cout<<convert<b,a>::param()<<std::endl;     std::cout<<convert<a,c>::param()<<std::endl;     std::cout<<convert<c,a>::param()<<std::endl;     std::cout<<convert<b,c>::param()<<std::endl;     std::cout<<convert<c,b>::param()<<std::endl;      convert<int,double>::param(); } 

the idea once give general declaration, , specify first case template arguments equal (that should give zero) case different, in converted parameter returned.

next, n classes, need give specializations n*(n-1)/2 convert classes. (in case needed, further simplified derivation, example).


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -