Can I overload functions with type-traits?

后端 未结 4 1520
南旧
南旧 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:51

    Two functions signatures are not allowed to differ only by the default value of a template parameter. What would happen if you explicitly called function< int, void >?

    Usual usage of enable_if is as the function return type.

    //Handle all types from Category 1
    template
    typename std::enable_if::value>::type
    function(T t){
        //do category 1 stuff to the type
        return;
    }
    
    //Handle all types from Category 2
    template
    typename std::enable_if::value>::type
    function(T t){
        //do category 2 stuff to the type
        return;
    }
    

提交回复
热议问题