【问题标题】:How to test lambda in C++11如何在 C++11 中测试 lambda
【发布时间】:2013-08-07 15:22:36
【问题描述】:

通常,要测试一个指针是否指向函数,使用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);
}

EDIT2:

类似问题:C++ metafunction to determine whether a type is callable

【问题讨论】:

  • 一个 lambda 对象只是一个函子。您目前如何处理函子?
  • 关于你的新代码:如果Func f 没有实现f(),那么你的代码将无法编译。您实际上想使用std::enable_if 之类的东西来使用此功能。 (虽然编译时错误可能被认为更好)。
  • @liuyanghejerry 你为什么要做这个测试?如果在特定实例化中Func 表示的类型上没有定义operator(),那么这是一个编译时错误,这肯定比运行时断言要好。
  • @liuyanghejerry 对在编译时验证的内容进行运行时检查是愚蠢的。如果调用f 无效,编译器不会编译f();,它会告诉你程序格式错误。 ...而@sharth 击败了我。
  • 顺便说一句,编译器可以推出不错的错误消息。 Clang 3.3 报告:“错误:调用的对象类型 'int' 不是函数或函数指针”。 GCC 可以使用一些工作,但对于 “错误:表达式不能用作函数” 仍然是合理的。当我执行deferJob(3) 之类的操作时,会出现这些错误。

标签: c++ c++11 lambda


【解决方案1】:

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

  • 要创建适用于仿函数、lambda 和传统函数的 type_trait,我想我会考虑看看模板​​参数是否可转换为 std::function&lt;void()&gt;。我认为这将清楚地涵盖大多数基础。

  • 正如我们在 cmets 中提到的,您不能像您正在做的那样测试模板参数。函数后面的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!";
}

【讨论】:

  • 谢谢,这是我原本想要的。虽然看起来编译错误明显更好。
猜你喜欢
  • 2012-08-27
  • 2012-08-27
  • 1970-01-01
  • 2012-09-20
  • 2011-06-18
  • 1970-01-01
  • 1970-01-01
  • 2016-01-26
相关资源
最近更新 更多