通常,要测试指针是否指向函数,使用std::is_function就足够了.
但是,它无法与lambda一起使用.因为lambda是一个对象operator().
现在我必须使用它们is_function并is_object检查一个是否像函数一样工作,如下所示:
std::is_function<decltype(f)>::value || std::is_object<decltype(f)>::value
所以我想知道是否有更好的方法来测试一个是否是lambda?
编辑:
相关代码:
template<typename Func>
void deferJob(Func f, int ms=2000)
{
if(! std::is_function<decltype(f)>::value
&& ! std::is_object<decltype(f)>::value){
qDebug()<<"Not function!";
return;
}
QTimer* t = new QTimer;
t->setSingleShot(true);
QObject::connect(t, &QTimer::timeout,
[&f, t](){
qDebug()<<"deferJob";
f();
t->deleteLater();
});
t->start(ms);
}
Run Code Online (Sandbox Code Playgroud)
EDIT2:
类似的问题:C++元函数确定一个类型是否可调用
所以这里有一些可能有用也可能没有帮助的想法.
要创建一个适用于仿函数,lambdas和传统函数的type_trait,我想我会研究模板参数是否可以转换为std::function<void()>.我认为这将以明确的方式涵盖大多数基地.
正如我们在评论中提到的那样,您无法像您一样测试模板参数.f()函数中的后面会导致编译错误,因此您将永远不会有机会看到运行时错误.
你可以试着做点什么std::enable_if.您需要创建模板特化,以便SFINAE可以用于选择正确的实现.这将使用我在子弹1中提到的type_trait.
如果您这样做,您可以将其他模板的实现设置static_assert为创建"更好"的错误消息.
话虽如此,编译器错误消息首先并没有那么糟糕.(至少在clang和gcc中.我没有看作msvc).
这不会给你一个很好的错误信息,但它会让你有一个不同的错误:
#include <cassert>
#include <functional>
#include <type_traits>
template <typename Func>
typename std::enable_if<std::is_convertible<Func, std::function<void()>>::value>::type
deferJob(Func f, int ms=2000) {
}
void normal_function() {}
int main() {
deferJob([]() {}); // works
deferJob(&normal_function); // works
deferJob(3); // compile time error
}
Run Code Online (Sandbox Code Playgroud)
在Clang中,我收到的错误如下:
foo.cc:15:2: error: no matching function for call to 'deferJob'
deferJob(3); // compile time error
^~~~~~~~
foo.cc:6:25: note: candidate template ignored: disabled by 'enable_if' [with Func = int]
typename std::enable_if<std::is_convertible<Func, std::function<void()>>::value>::type
Run Code Online (Sandbox Code Playgroud)
在GCC中,我收到的错误如下:
foo.cc: In function ‘int main()’:
foo.cc:15:12: error: no matching function for call to ‘deferJob(int)’
deferJob(3); // compile time error
^
foo.cc:15:12: note: candidate is:
foo.cc:7:1: note: template<class Func> typename std::enable_if<std::is_convertible<Func, std::function<void()> >::value>::type deferJob(Func, int)
deferJob(Func f, int ms=2000) {
^
foo.cc:7:1: note: template argument deduction/substitution failed:
foo.cc: In substitution of ‘template<class Func> typename std::enable_if<std::is_convertible<Func, std::function<void()> >::value>::type deferJob(Func, int) [with Func = int]’:
foo.cc:15:12: required from here
foo.cc:7:1: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
Run Code Online (Sandbox Code Playgroud)
我们可以更进一步(虽然这样做很难进一步扩展)并添加一个额外的功能:
template <typename Func>
typename std::enable_if<not std::is_convertible<Func, std::function<void()>>::value>::type
deferJob(Func f, int ms=2000) {
static_assert(false, "You should pass a function");
}
Run Code Online (Sandbox Code Playgroud)
这导致clang报告(在编译时):
foo.cc: In function ‘typename std::enable_if<(! std::is_convertible<Func, std::function<void()> >::value)>::type deferJob(Func, int)’:
foo.cc:14:2: error: static assertion failed: You should pass a function
static_assert(false, "You should pass a function");
Run Code Online (Sandbox Code Playgroud)
但遗憾的是,它没有给出堆栈跟踪,所以我发现它远没有任何早期的消息有用.
最后,我们还可以用运行时消息替换静态断言:
template <typename Func>
typename std::enable_if<not std::is_convertible<Func, std::function<void()>>::value>::type
deferJob(Func f, int ms=2000) {
qDebug() << "Not function!";
}
Run Code Online (Sandbox Code Playgroud)