【发布时间】:2021-06-26 16:44:07
【问题描述】:
我有一个函数模板,如下面的最小 sn-p。我想在拨打电话之前测试它是否可以调用:
#include <concepts>
void f(auto a) {}
// void f(int a) {} // This is ok
static_assert(std::invocable<decltype(f), int>);
但编译时不会出错
error: 'decltype' cannot resolve address of overloaded function
或者,
void f(auto a) {}
template <auto F> concept callable = requires { {F(27)}; };
static_assert(callable<f>);
报错
error: unable to deduce 'auto' from 'f'
这是 C++20 语言的限制吗?有没有办法强制实例化f<int>?是否有替代方法可以在不更改 f 的情况下编译检查?
【问题讨论】:
-
¿ 为什么不直接使用
f<int>来实例化? -
因为概念在头文件中并且不知道
int或行为类似于int的类。 -
@Arjonais:他在问你为什么不把它调用为
callable<f<int>>。f是一个模板;f<int>是一个函数。或者更确切地说,is_invokable<decltype(f<int>), int>. -
¿“不知道 int”是什么意思?您正在使用
int作为std::invocable的第二个参数。
标签: c++ templates function-pointers c++20 callable