如何在C++11中测试lambda

How to test lambda in C++11

本文关键字:测试 lambda C++11      更新时间:2023-10-16

通常,要测试指针是否指向函数,使用std::is_function就足够了。

但是,它不能与lambda一起使用。由于lambda是一个具有operator()

现在我必须同时使用is_functionis_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!";
}