久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

我應(yīng)該在 C++ 中使用 std::function 還是函數(shù)指針?

Should I use std::function or a function pointer in C++?(我應(yīng)該在 C++ 中使用 std::function 還是函數(shù)指針?)
本文介紹了我應(yīng)該在 C++ 中使用 std::function 還是函數(shù)指針?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

在C++中實現(xiàn)回調(diào)函數(shù)時,我是否還應(yīng)該使用C風格的函數(shù)指針:

When implementing a callback function in C++, should I still use the C-style function pointer:

void (*callbackFunc)(int);

或者我應(yīng)該使用 std::function:

Or should I make use of std::function:

std::function< void(int) > callbackFunc;

推薦答案

簡而言之,除非有理由不使用,否則請使用 std::function.

函數(shù)指針的缺點是不能捕獲某些上下文.例如,您將無法將 lambda 函數(shù)作為捕獲某些上下文變量的回調(diào)傳遞(但如果它不捕獲任何上下文變量,它將起作用).因此,調(diào)用對象的成員變量(即非靜態(tài))也是不可能的,因為需要捕獲對象(this-pointer).(1)

Function pointers have the disadvantage of not being able to capture some context. You won't be able to for example pass a lambda function as a callback which captures some context variables (but it will work if it doesn't capture any). Calling a member variable of an object (i.e. non-static) is thus also not possible, since the object (this-pointer) needs to be captured.(1)

std::function(自 C++11 起)主要用于存儲一個函數(shù)(傳遞它不需要存儲它).因此,如果您想將回調(diào)存儲在例如成員變量中,這可能是您的最佳選擇.而且如果你不存儲它,它也是一個不錯的首選"盡管它的缺點是在調(diào)用時引入了一些(非常小的)開銷(因此在非常關(guān)鍵的性能情況下它可能是一個問題,但在大多數(shù)情況下它不應(yīng)該).它非常通用":如果您非常關(guān)心一致和可讀的代碼,并且不想考慮您所做的每一個選擇(即想要保持簡單),請使用 std::function 對于您傳遞的每個函數(shù).

std::function (since C++11) is primarily to store a function (passing it around doesn't require it to be stored). Hence if you want to store the callback for example in a member variable, it's probably your best choice. But also if you don't store it, it's a good "first choice" although it has the disadvantage of introducing some (very small) overhead when being called (so in a very performance-critical situation it might be a problem but in most it should not). It is very "universal": if you care a lot about consistent and readable code as well as don't want to think about every choice you make (i.e. want to keep it simple), use std::function for every function you pass around.

考慮第三種選擇:如果您要實現(xiàn)一個小函數(shù),然后通過提供的回調(diào)函數(shù)報告某些內(nèi)容,請考慮一個模板參數(shù),它可以是任何可調(diào)用的對象,即一個函數(shù)指針,一個函子,一個 lambda,一個 std::function,......這里的缺點是你的(外部)函數(shù)變成了一個模板,因此需要在標題中實現(xiàn).另一方面,您可以獲得可以內(nèi)聯(lián)回調(diào)調(diào)用的優(yōu)勢,因為您的(外部)函數(shù)的客戶端代碼看到"了對回調(diào)的調(diào)用將提供可用的確切類型信息.

Think about a third option: If you're about to implement a small function which then reports something via the provided callback function, consider a template parameter, which can then be any callable object, i.e. a function pointer, a functor, a lambda, a std::function, ... Drawback here is that your (outer) function becomes a template and hence needs to be implemented in the header. On the other hand you get the advantage that the call to the callback can be inlined, as the client code of your (outer) function "sees" the call to the callback will the exact type information being available.

帶有模板參數(shù)的版本示例(對于 pre-C++11,編寫 & 而不是 &&):

Example for the version with the template parameter (write & instead of && for pre-C++11):

template <typename CallbackFunction>
void myFunction(..., CallbackFunction && callback) {
    ...
    callback(...);
    ...
}


如下表所示,它們各有優(yōu)缺點:


As you can see in the following table, all of them have their advantages and disadvantages:

<頭>
函數(shù)ptrstd::function模板參數(shù)
可以捕獲上下文變量no1
沒有調(diào)用開銷(見評論)no
可以內(nèi)聯(lián)(見評論)nono
可以存放在一個類成員中no2
可以在header之外實現(xiàn)no
不支持 C++11 標準no3
可讀性很好(我認為)no(是)


(1) 存在克服此限制的解決方法,例如將附加數(shù)據(jù)作為進一步的參數(shù)傳遞給您的(外部)函數(shù):myFunction(..., callback, data) 將調(diào)用 callback(data).這是 C 風格的帶參數(shù)回調(diào)",這在 C++ 中是可能的(順便說一下,在 WIN32 API 中大量使用)但應(yīng)該避免,因為我們在 C++ 中有更好的選擇.


(1) Workarounds exist to overcome this limitation, for example passing the additional data as further parameters to your (outer) function: myFunction(..., callback, data) will call callback(data). That's the C-style "callback with arguments", which is possible in C++ (and by the way heavily used in the WIN32 API) but should be avoided because we have better options in C++.

(2) 除非我們談?wù)摰氖穷惸0?,即存儲函?shù)的類是模板.但這意味著在客戶端,函數(shù)的類型決定了存儲回調(diào)的對象的類型,這在實際用例中幾乎從來不是一個選項.

(3) 對于 C++11 之前的版本,使用 boost::function

這篇關(guān)于我應(yīng)該在 C++ 中使用 std::function 還是函數(shù)指針?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 国产激情片在线观看 | 精品国产aⅴ | 亚洲一二三区在线观看 | 国产成人精品视频在线观看 | 亚洲综合一区二区三区 | 影音先锋亚洲资源 | 国产一区久久 | 一区在线视频 | 在线看片国产 | 亚洲欧美日韩激情 | 亚洲精选久久 | 美女黄网 | 国产精品精品 | 国产成人免费观看 | 国产精品视频在线播放 | 国产欧美精品区一区二区三区 | 亚洲国产精品久久久 | 久久精品久久久 | 九色网址 | 欧美一区二区免费视频 | 亚洲精品福利视频 | 99re视频在线观看 | 国产第一亚洲 | 欧美精品一二三 | www.色.com| 久久亚洲综合 | 久久亚洲一区二区三区四区 | 2018国产大陆天天弄 | 天天操人人干 | 国产精品欧美一区二区三区不卡 | 欧洲一区视频 | 成人免费一区二区三区视频网站 | 日韩在线视频免费观看 | 欧洲精品久久久久毛片完整版 | 日韩欧美一区二区三区 | 亚洲久久一区 | 日韩精品一区二区三区免费观看 | 69亚洲精品 | 精品一区二区久久久久久久网站 | 国产91在线播放 | 国产精品污污视频 |