如何在C++11中测试lambda
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);
}
第2版:
类似的问题: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
}
在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!";
}
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 可组合的lambda/std::函数与std::可选
- 尽管测试成功,CppUnit测试核心仍被丢弃.为什么
- 数据成员SFINAE的C++17测试:gcc vs clang
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何建立使用模板函数的lambda函数的尾部返回类型
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么二进制搜索在我的测试中不起作用
- 从父数组测试用例构造二叉树失败
- 试图对缓存进行跨步测试,但程序并没有结束
- 编写可测试的代码 - lambda 函数和unique_ptr中的basic_istream工厂
- 在QT中测试Lambda唯一连接
- C++ lambda 测试拦截器/模拟
- 处理重构、模板 SFINAE 测试和 lambda
- 排序测试模板化函数 lambda:非法使用此类型作为表达式
- 测试 lambda 是否为无状态
- C++测试lambda是否正常工作
- 如何在C++11中测试lambda