ant*_*_rh 7 c++ recursion templates sfinae overload-resolution
让我们创建柯里化函数。
template <typename TFunc, typename TArg>
class CurryT
{
public:
CurryT(const TFunc &func, const TArg &arg)
: func(func), arg(arg )
{}
template <typename... TArgs>
decltype(auto) operator()(TArgs ...args) const
{ return func(arg, args...); }
private:
TFunc func;
TArg arg ;
};
template <typename TFunc, typename TArg>
CurryT<decay_t<TFunc>, remove_cv_t<TArg>>
Curry(const TFunc &func, const TArg &arg)
{ return {func, arg}; }
Run Code Online (Sandbox Code Playgroud)
以及将函数解耦为单参数函数的函数:
// If single argument function (F(int)).
template <typename F>
static auto Decouple(const F &f, enable_if_t<is_invocable_v<F, int>> * = nullptr)
{
return f;
}
// If multiple arguments function (F(int, int, ...)).
template <typename F>
static auto Decouple(const F &f, enable_if_t<!is_invocable_v<F, int>> * = nullptr)
{
return [f](int v) { return Decouple( Curry(f, v) ); };
}
Run Code Online (Sandbox Code Playgroud)
如果传递 2 个参数函数,则一切正常:
auto f1 = Decouple(
[](int a, int b)
{ std::cout << a << " " << b << std::endl; }
);
f1(3)(4); // Outputs 3 4
Run Code Online (Sandbox Code Playgroud)
但如果我添加更多参数
auto f2 = Decouple(
[](int a, int b, int c)
{ std::cout << a << " " << b << " " << c << std::endl; }
);
f(5)(6)(7);
Run Code Online (Sandbox Code Playgroud)
编译中断:https://coliru.stacked-crooked.com/a/10c6dba670d17ffa
main.cpp: In instantiation of 'decltype(auto) CurryT<TFunc, TArg>::operator()(TArgs ...) const [with TArgs = {int}; TFunc = main()::<lambda(int, int, int)>; TArg = int]':
main.cpp:17:26: error: no match for call to '(const main()::<lambda(int, int, int)>) (const int&, int&)'
17 | { return func(arg, args...); }
Run Code Online (Sandbox Code Playgroud)
它在 的实例化中中断std::is_invocable。
由于调试标准库很困难,我创建了标准类型特征类的简单版本:
template <typename F> true_type check(const F &, decltype( declval<F>()(1) )* );
template <typename F> false_type check(const F &, ...);
template <typename F>
struct invocable_with_int : decltype(check(declval<F>(), nullptr))
{};
template <typename F>
inline constexpr bool invocable_with_int_v = invocable_with_int<F>::value;
template<bool B>
struct my_enable_if {};
template<>
struct my_enable_if<true>
{ using type = void; };
template <bool B>
using my_enable_if_t = typename my_enable_if<B>::type;
Run Code Online (Sandbox Code Playgroud)
问题仍然相同https://coliru.stacked-crooked.com/a/722a2041600799b0:
main.cpp:29:73: required by substitution of 'template<class F> std::true_type check(const F&, decltype (declval<F>()(1))*) [with F = CurryT<main()::<lambda(int, int, int)>, int>]'
Run Code Online (Sandbox Code Playgroud)
它尝试解决对此函数的调用:
template <typename F> true_type check(const F &, decltype( declval<F>()(1) )* );
Run Code Online (Sandbox Code Playgroud)
但decltype (declval<F>()(1))*)失败了。但是,是否应该因为模板替换失败而从重载解析中删除此函数?Decouple它在第一次调用时起作用。但是当第二次调用时,SFINAE 似乎被禁用,并且模板替换的第一次失败给出了编译错误。二次 SFINAE 有限制吗?为什么递归调用模板函数不起作用?
该问题在 GCC 和 Clang 中重现。所以这不是编译器错误。
use*_*522 10
您的operator()重载完全不受约束,因此声称可以使用任何参数集进行调用。仅检查声明(而不是定义)来确定在重载决策中调用哪个函数。如果替换到定义中失败,则 SFINAE 不适用。
因此,限制您的operator()要求TFunc可以使用TArg和TArgs...作为参数进行调用。
例如:
template <typename... TArgs>
auto operator()(TArgs ...args) const -> decltype(func(arg, args...))
Run Code Online (Sandbox Code Playgroud)