在传递函数的标识符时,无法检查模板参数是否是您遇到的问题 [英] Having trouble checking whether template parameter is something you get when you pass a function's identifier
问题描述
我要实现以下功能:
template<typename Function, typename... Parameters>
inline void foo(
const Function& kernel_function,
bar_t bar
Parameters... parameters)
{
static_assert(/* magic */,
"You can only foo() a function, not values of any other type");
/ * etc. etc. */
}
,我只需要使用函数的标识符或指向函数的指针来调用它:不能使用lambads或具有operator()
或std::function
s的方法或类.我应该用什么代替/* magic */
?仅使用std::is_function
似乎无效.
and I need it to only be called with the identifiers of functions, or with pointers to functions: No lambads or methods or classes with operator()
or std::function
s. What should I replace /* magic */
with? Just using std::is_function
doesn't seem to work.
推荐答案
在 std::is_function
,如果返回true
您传递给它一个实际的函数和带有lambda的false
,带有重载的operator()
的类以及指向函数的指针.我们也有 std::is_pointer
和
In <type_traits>
we have std::is_function
which returns true
if you pass it an actual function and false
with lambdas, classes with overloaded operator()
and pointers to functions. We also have std::is_pointer
and std::remove_pointer
which can be used to check for and remove the pointer type from a function pointer to test the pointer with std::is_function
. Using those your assert would look like
static_assert(std::is_function<Function>::value ||
(std::is_pointer<Function>::value &&
std::is_function<std::remove_pointer<Function>::type>::value),
"You can only foo() a function, not values of any other type");
这篇关于在传递函数的标识符时,无法检查模板参数是否是您遇到的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!