Why is SFINAE causing failure when there are two f

2020-08-02 05:53发布

I was trying to wrap my head around this question here because it was written in such a way that it was hiding what it was actually doing. So I rewrote it as such:

template<typename CLASS>
struct has_begin
{
    // NOTE: sig_matches() must come before fn_exists() as it is used for its
    //       type.  Also, no function bodies are needed as they are never called.
    
    // This matching sig results in a return type of true_type
    template<typename A_CLASS>
    static auto
        sig_matches(void(A_CLASS::*)())
        -> std::true_type;
    
    // If the member function A_CLASS::begin exists and a sig_matches() function
    // exists with the required sig, then the return type is the return type of
    // sig_matches(), otherwise this function can't exist because at least one
    // the types don't exist so match against fn_exists(...).
    template <typename A_CLASS>
    static auto
        fn_exists(decltype(&A_CLASS::begin))          
        -> decltype(sig_matches<A_CLASS>(&A_CLASS::begin));

    // Member function either doesn't exist or doesn't match against a 
    // sig_matches() function.
    template<typename A_CLASS>
    static auto
        fn_exists(...)
        -> std::false_type;

    // Intermediate storage of type for clarity
    typedef decltype(fn_exists<CLASS>(nullptr)) type;

    // Storing the resulting value
    static int const value = type::value;
};

After doing that, it was fairly easy what was happening. However, I found something odd. If a class was passed to this with 2 begin signatures, one of which matched against has_begin::sig_matches(), it would fail to match against it.

#include <iostream>
#include <type_traits>
struct A
{
    void begin()
    {
        std::cout << "begin() called 1" << std::endl;
    }
};

struct B {};

struct C
{
    void begin()
    {
        std::cout << "begin() called 1" << std::endl;
    }

    void begin(float)
    {
        std::cout << "begin() called 2" << std::endl;
    }
};

template<typename T, typename...ARGs>
typename std::enable_if<!!has_begin<T>::value>::type
    call(ARGs...args)
{
    std::cout << "Found(" << has_begin<T>::value << ")" << std::endl;
    T().begin(args...);
}

template<typename T, typename...ARGs>
typename std::enable_if<!has_begin<T>::value>::type
    call(ARGs...)
{
    std::cout << "NOT Found(" << has_begin<T>::value << ")" << std::endl;
}

int main()
{
    call<A>(); // A::begin() called
    call<B>(); // B has no begin()
    call<C>(); // C::begin() is not called.
    return 0;
}

demo

Why is it failing to match against C::begin()?

Edit

The reason is that &A_CLASS::begin is ambiguous. The corrected class is as follows:

template<typename CLASS>
struct has_begin
{
    // NOTE: No function bodies are needed as they are never called.
    
    // If the member function A_CLASS::begin exists with the required sig,
    // then the return type is true_type otherwise this function can't
    // exist because the type cannot be deduced.
    template <typename A_CLASS>
    static auto
        fn_exists(decltype((void(A_CLASS::*)())&A_CLASS::begin))          
        -> std::true_type;

    // Member function either doesn't exist or doesn't match against the
    // required signature
    template<typename A_CLASS>
    static auto
        fn_exists(...)
        -> std::false_type;

    // Intermediate storage of type for clarity
    typedef decltype(fn_exists<CLASS>(nullptr)) type;

    // Storing the resulting value
    static int const value = type::value;
};

Yakk and dyp brought up a good point. Here is a way to do the same but with a compatible signature:

template<typename CLASS>
struct has_begin
{
    // NOTE: No function bodies are needed as they are never called.

    // If the member function A_CLASS::begin exists that has a compatible sig, 
    // then the return type is true_type otherwise this function can't exist
    // because the type cannot be deduced.
    template <typename A_CLASS>
    static auto
        fn_exists(decltype(std::declval<A_CLASS>().begin())*)          
        -> std::true_type;

    // Member function either doesn't exist or doesn't match against the
    // required compatible signature
    template<typename A_CLASS>
    static auto
        fn_exists(...)
        -> std::false_type;

    // Intermediate storage of type for clarity
    typedef decltype(fn_exists<CLASS>(nullptr)) type;

    // Storing the resulting value
    static int const value = type::value;
};

I find this cleaner than Yakks answer as it doesn't require detail namespaces and other 'noise', but YYMV.

标签: c++ c++11 sfinae
2条回答
放我归山
2楼-- · 2020-08-02 06:08

This is C++11. Stop doing that C++03 gymnastics.

// bundle of types:
template<class...>struct types{using type=types;};

// comes in std in C++14 or 1z, but easy to write here:
template<class...>struct voider{using type=void;};
template<class...Ts>using void_t=typename voider<Ts...>::type;

// hide the SFINAE stuff in a details namespace:
namespace details {
  template<template<class...>class Z, class types, class=void>
  struct can_apply : std::false_type {};
  template<template<class...>class Z, class...Ts>
  struct can_apply<Z,types<Ts...>,void_t<
    Z<Ts...>
  >>:std::true_type{};
}
// can_apply<template, types...> is true
// iff template<types...> is valid:
template<template<class...>class Z, class...Ts>
using can_apply=details::can_apply<Z,types<Ts...>>;

the above is write-once boilerplate. It gives you a can_apply<template, Ts...> helper that makes writing "do we have a method" and other similar tests easy:

// the type of X.begin(), in a SFINAE friendly manner:
template<class X>
using begin_result = decltype( std::declval<X>().begin() );

// Turn the above into a "is there a begin" test:
template<class X>
using has_begin = can_apply< begin_result, X >;

and now has_begin<X>{} is true if and only if X can be called with .begin().

This also fixes a flaw in your code.

struct foo {
  void begin(double x = 0.0) {}
};

will fail your test, but pass mine.

查看更多
成全新的幸福
3楼-- · 2020-08-02 06:11

Replace

// If the member function A_CLASS::begin exists and a sig_matches() function
// exists with the required sig, then the return type is the return type of
// sig_matches(), otherwise this function can't exist because at least one
// the types don't exist so match against fn_exists(...).
template <typename A_CLASS>
static auto
    fn_exists(decltype(&A_CLASS::begin))          
    -> decltype(sig_matches<A_CLASS>(&A_CLASS::begin));

By

// If the member function A_CLASS::begin exists and a sig_matches() function
// exists with the required sig, then the return type is the return type of
// sig_matches(), otherwise this function can't exist because at least one
// the types don't exist so match against fn_exists(...).
template <typename A_CLASS>
static auto
    fn_exists(std::nullptr_t)          
    -> decltype(sig_matches<A_CLASS>(&A_CLASS::begin));

As

decltype(&A_CLASS::begin) is ambiguous when there are overloads for `begin`.

Live demo

查看更多
登录 后发表回答