[英]How to test lambda in C++11
通常,要测试一个指针是否指向函数,使用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);
}
EDIT2:
类似的问题: C ++元函数确定一个类型是否可调用
所以这里有一些可能有用或可能没有用的想法。
要创建一个适用于仿函数,lambda和传统函数的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
}
在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
在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>’
我们可以更进一步(虽然这样做很难进一步扩展)并添加一个额外的功能:
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");
}
这导致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");
但遗憾的是,它没有给出堆栈跟踪,所以我发现它远没有任何早期的消息有用。
最后,我们还可以用运行时消息替换静态断言:
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!";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.