簡體   English   中英

如何在C ++ 11中測試lambda

[英]How to test lambda in C++11

通常,要測試一個指針是否指向函數,使用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 ++元函數確定一個類型是否可調用

所以這里有一些可能有用或可能沒有用的想法。

  • 要創建一個適用於仿函數,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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM