【发布时间】:2023-03-09 06:32:01
【问题描述】:
我正在尝试修改 is_detected 成语以允许将可变参数传递给它。我需要这个,因为我检测到的一些成员函数将具有用户提供的参数。
到目前为止,这就是我的工作。您将额外的参数提供给is_detected_args_v,理论上,模板专业化会启动并正确编译。从而给std::true_type。
#include <type_traits>
#include <cstdio>
// slightly modified (and simplified) is_detected
template <template <class, class...> class Op, class T, class = void, class...>
struct is_detected_args : std::false_type {};
template <template <class, class...> class Op, class T, class... Args>
struct is_detected_args<Op, T, std::void_t<Op<T, Args...>>, Args...>
: std::true_type {};
template <template <class, class...> class Op, class T, class... Args>
inline constexpr bool is_detected_args_v
= is_detected_args<Op, T, Args...>::value;
// has_func, checks the function starts with int, and then Args&...
template <class T, class... Args>
using has_func = decltype(std::declval<T>().func(
std::declval<int>(), std::declval<Args&>()...));
// has the func
struct obj {
void func(int, double&, double&) {
printf("potato\n");
}
};
int main(int, char**) {
obj o;
if constexpr(is_detected_args_v<has_func, obj, double, double>) {
double d = 0;
double d2 = 42;
o.func(42, d, d2);
}
}
您可以在此处运行示例(在所有 3 个编译器上测试):https://wandbox.org/permlink/ttCmWSVl1XVZjty7
问题是,永远不会选择专业化并且条件总是错误的。我的问题有两个方面。
- 这可能吗?
- 为什么
is_detected不专业化?
谢谢
【问题讨论】:
标签: c++ templates c++17 template-meta-programming sfinae