Can I overload functions with type-traits?

后端 未结 4 1523
南旧
南旧 2021-02-05 17:15

Let\'s say, I have six types, and they each belong in a conceptual category.
Here is a diagram that shows this:

<script

4条回答
  •  既然无缘
    2021-02-05 17:41

    As an alternative to category selection via "traits", you can also consider CRTP (where the type carries the category as a base):

    template class category1 {};
    template class category2 {};
    
    class A1: public category1 { ..... };
    class A2: public category2 { ..... };
    class B1: public category1 { ..... };
    class B2: public category2 { ..... };
    
    templatevoid funcion_on1(category1& st)
    {
       T& t = static_cast(st);
       .....
    }
    
    templatevoid funcion_on1(category2& st)
    {
       T& t = static_cast(st);
       .....
    }
    

    The advantage is to have a less polluted namespace.

提交回复
热议问题