当有两个具有不同签名的函数时,为什么SFINAE会导致失败?

时间:2015-04-21 12:44:58

标签: c++ c++11 sfinae

我试图绕过这个问题here,因为它是以隐藏其实际行为的方式编写的。所以我重写了它:

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;
};

在这样做之后,发生的事情相当容易。但是,我发现了一些奇怪的东西如果一个类通过2个开始签名传递给它,其中一个与has_begin::sig_matches()匹配,则它将无法匹配它。

#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

为什么无法与C::begin()匹配?

修改

原因是&A_CLASS::begin含糊不清。更正后的课程如下:

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和dyp提出了一个很好的观点。这是一种方法,但使用兼容的签名:

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;
};

我觉得这比Yakks的答案更清晰,因为它不需要细节命名空间和其他'噪音',但是YYMV。

2 个答案:

答案 0 :(得分:5)

这是C ++ 11。停止做C ++ 03体操。

// 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...>>;

以上是一次性写入样板。它为您提供了一个can_apply<template, Ts...>帮助器,使得“我们有一个方法”和其他类似的测试变得容易:

// 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 >;
当且仅当可以使用has_begin<X>{}调用X时,

现在.begin()为真。

这也解决了代码中的缺陷。

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

将失败你的考试,但通过我的。

答案 1 :(得分:3)

替换

// 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));

通过

// 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));

作为

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

Live demo

相关问题