c++ - 如何在 C++11 中测试 lambda

标签 c++ c++11 lambda

通常,要测试指针是否指向函数,请使用 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++ metafunction to determine whether a type is callable

最佳答案

所以这里有一些想法,可能有用也可能没用。

  • 要创建适用于仿函数、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!";
}

关于c++ - 如何在 C++11 中测试 lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18107368/

相关文章:

c++ - 线程退出事件 - C++

c++ - 如何使用rapidxml创建xml节点

java - 如何在 Java 8 中迭代 lambda 过滤器流中的列表?

java - 每次按下按钮时呈现颜色(lambda 表达式)

C++11 lambda 捕获列表引用

c++ - DirectX 多个 DLL 版本?

c++ - 是否有相当于 slmgr.vbs/skms 的 C++/Winapi

c++ - 为什么重载运算符&&错误?

c++11 - 在 C++11 中遍历 UTF-8 字符串

c++ - 模板类的模板 friend 的问题