尝试使用具有尾随返回类型的 lambda 进行 SFINAE 时出现硬错误

Hard error when attempting SFINAE with lambda that has trailing return type

本文关键字:SFINAE 进行 lambda 硬错误 返回类型      更新时间:2023-10-16

我在SO上看到了解释为什么SFINAE不适用于lambda返回类型的答案。我稍微改变了我的设计,但现在收到了一个更奇怪的错误。我不知道我错过了什么。代码将使用 C++11、14 和 17 进行编译。我有一个兼容C++17的apply.

#include <algorithm>
#include <vector>
#include <functional>
#include <tuple>
#include <utility>
template <typename ...T>
struct S {
std::vector<std::tuple<T...>> things;
template <typename F>
typename std::enable_if<std::is_same<
typename std::invoke_result<
std::apply<F, T...>, F, T...>::type, bool>::value,
bool>::type
find_if(const F &f) {
return std::any_of(things.begin(), things.end(),
[&](const std::tuple<T...> &t) {
return std::apply(f, t);
});
}
template <typename F>
auto find_if(const F &f) -> decltype(std::declval<F>()(things.front())) {
return std::any_of(things.begin(), things.end(),
[&](const std::tuple<T...> &t) { return f(t); });
}
};
void f() {
S<int, float> s;
auto l = [](const std::tuple<int, float> &) -> bool { return false; };
s.find_if(l);
}

我的目的是调用正确的谓词。如果谓词具有std::tuple<Ts...>类型的单个参数,则直接调用它。如果谓词参数列表与解压缩的模板参数包匹配,请调用该参数包。

GCC和Clang都抱怨第一种方法,其中apply不是一种类型。

<source>:13:26: error: type/value mismatch at argument 1 in template parameter list for 'template<class _Functor, class ... _ArgTypes> struct std::invoke_result'
13 |       std::apply, F, T...>::type, bool>::value,
|                          ^
<source>:13:26: note:   expected a type, got 'std::apply'

元函数std::invoke_result期望它的第一个参数是可调用的东西的类型,以及参数的类型。

不幸的是,std::apply不是一种类型,而是一个函数。此外,std::apply使用类型推导,因此它对 sfinae 不友好。

解决方案是使用可调用对象的类型,而不是要应用的调用。

template <typename ...T>
struct S {
std::vector<std::tuple<T...>> things;
template <typename F, typename std::enable_if_t<
std::is_same_v<std::invoke_result_t<F, T...>, bool>, int> = 0>
auto find_if(const F &f) -> bool {
return std::any_of(things.begin(), things.end(),
[&](const std::tuple<T...> &t) {
return std::apply(f, t);
});
}
};

此外,您的 lambda 应如下所示:

[](int, float) -> bool { return false; };

这是因为 apply 正在分解元组。