SFINAE working in return type but not as template parameter

后端 未结 3 2013
情深已故
情深已故 2020-11-22 14:20

I already used the SFINAE idiom quite a few times and I got used to put my std::enable_if<> in template parameters rather than in return types. However, I

相关标签:
3条回答
  • 2020-11-22 14:51

    The = ... of the template just gives a default parameter. This is not part of the actual signature which looks like

    template<typename T, typename>
    auto foo(T a);
    

    for both functions.

    Depending on your needs, the most generic solution for this problem is using tag dispatching.

    struct integral_tag { typedef integral_tag category; };
    struct floating_tag { typedef floating_tag category; };
    
    template <typename T> struct foo_tag
    : std::conditional<std::is_integral<T>::value, integral_tag,
                        typename std::conditional<std::is_floating_point<T>::value, floating_tag,
                                                   std::false_type>::type>::type {};
    
    template<typename T>
    T foo_impl(T a, integral_tag) { return a; }
    
    template<typename T>
    T foo_impl(T a, floating_tag) { return a; }
    
    template <typename T>
    T foo(T a)
    {
      static_assert(!std::is_base_of<std::false_type, foo_tag<T> >::value,
                     "T must be either floating point or integral");
      return foo_impl(a, typename foo_tag<T>::category{});
    }
    
    struct bigint {};
    template<> struct foo_tag<bigint> : integral_tag {};
    
    int main()
    {
      //foo("x"); // produces a nice error message
      foo(1);
      foo(1.5);
      foo(bigint{});
    }
    
    0 讨论(0)
  • 2020-11-22 14:52

    You should take a look at 14.5.6.1 Function template overloading (C++11 standard) where function templates equivalency is defined. In short, default template arguments are not considered, so in the 1st case you have the same function template defined twice. In the 2nd case you have expression referring template parameters used in the return type (again see 14.5.6.1/4). Since this expression is part of signature you get two different function template declarations and thus SFINAE get a chance to work.

    0 讨论(0)
  • 2020-11-22 14:52

    Values in the templates work:

    template<typename T,
             typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
    auto foo(T)
        -> void
    {
        std::cout << "I'm an integer!\n";
    }
    
    template<typename T,
             typename std::enable_if<std::is_floating_point<T>::value, int>::type = 0>
    auto foo(T)
        -> void
    {
        std::cout << "I'm a floating point number!\n";
    }
    
    0 讨论(0)
提交回复
热议问题