为什么我们可以检测 SFINAE 中 operator() 的默认参数值的存在,而不是自由函数和 PMF 的默认参数值



在下面的程序中,案例 1 尝试通过指向成员函数的指针使用默认参数。案例 2 尝试通过函数引用使用默认参数。案例 3 使用 operator() 中的默认参数。这里唯一有趣的断言是使用别名can_call_with_one的断言 - 其他断言的存在是为了证明设置的正确性。

在我可用的最新版本的 GCC、Clang 和 MSVC 中,该程序在情况 1 和 2 中失败了单参数断言。

我的问题是双重的:

  1. 这些结果是否符合ISO C++标准?
  2. 如果是这样,为什么案例 3 没有失败?
#include <type_traits>
#include <utility>
struct substitution_failure {};
substitution_failure check(...);
template<typename Pmf, typename T, typename... Args>
auto check(Pmf pmf, T t, Args&&... args) ->
    decltype((t.*pmf)(std::forward<Args>(args)...))*;
template<typename Fn, typename... Args>
auto check(Fn&& f, Args&&... args) ->
    decltype(f(std::forward<Args>(args)...))*;
template<typename T>
using test_result = std::integral_constant<bool,
    !std::is_same<T, substitution_failure>::value
>;
template<typename... Ts>
auto can_invoke(Ts&&... ts) ->
    test_result<decltype(check(std::forward<Ts>(ts)...))>;
namespace case_1 {
    //pointer to member function
    struct foo {
        int bar(int, int = 0);
    };
    using can_call_with_one = decltype(can_invoke(&foo::bar, foo{}, 0));
    using can_call_with_two = decltype(can_invoke(&foo::bar, foo{}, 0, 0));
    using can_call_with_three = decltype(can_invoke(&foo::bar, foo{}, 0, 0, 0));
    static_assert(can_call_with_one{}, "case 1 - can't call with one argument");
    static_assert(can_call_with_two{}, "case 1 - can't call with twp arguments");
    static_assert(!can_call_with_three{}, "case 1 - can call with three arguments");
}
namespace case_2 {
    //function reference
    int foo(int, int = 0);
    using can_call_with_one = decltype(can_invoke(foo, 0));
    using can_call_with_two = decltype(can_invoke(foo, 0, 0));
    using can_call_with_three = decltype(can_invoke(foo, 0, 0, 0));
    static_assert(can_call_with_one{}, "case 2 - can't call with one argument");
    static_assert(can_call_with_two{}, "case 2 - can't call with two arguments");
    static_assert(!can_call_with_three{}, "case 2 - can call with three arguments");
}

namespace case_3 {
    //function object
    struct foo {
        int operator()(int, int = 0);
    };
    using can_call_with_one = decltype(can_invoke(foo{}, 0));
    using can_call_with_two = decltype(can_invoke(foo{}, 0, 0));
    using can_call_with_three = decltype(can_invoke(foo{}, 0, 0, 0));
    static_assert(can_call_with_one{}, "case 3 - can't call with one argument");
    static_assert(can_call_with_two{}, "case 3 - can't call with two arguments");
    static_assert(!can_call_with_three{}, "case 3 - can call with three arguments");
}
int main() { return 0; }

可运行版本

函数的类型特征不附带默认参数的信息。如果是这样,您将无法使用默认值分配函数的指针,例如:

void foo(int, int = 0) {...}

自:

void(*fp)(int, int);
fp = &foo;

现在的问题是语言是否允许这样做 - 给定参数的默认值是否也应该标识函数的类型?这意味着参数的默认值应该是constexpr,因此会限制默认值的可用性。对于类型为 const char * 的示例参数,这样就不能内联定义默认值...

另一方面,如果函数的类型仅携带给定参数具有默认值的信息,而不知道值本身 - 编译器将无法在函数调用时从指针重建函数的默认值。

最新更新