Can I overload functions with type-traits?

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

    I think using tag despatch would be easier than SFINAE.

    template
    struct Category;
    
    template<>
    struct Category : std::integral_constant {};
    template<>
    struct Category : std::integral_constant {};
    template<>
    struct Category : std::integral_constant {};
    
    template<>
    struct Category : std::integral_constant {};
    template<>
    struct Category : std::integral_constant {};
    template<>
    struct Category : std::integral_constant {};
    
    template
    void foo(std::integral_constant, T x)
    {
        // Category 1 types.
    }
    
    template
    void foo(std::integral_constant, T x)
    {
        // Category 2 types.
    }
    
    template
    void foo(T x)
    {
        foo(Category(), x);
    }
    

提交回复
热议问题