久久久久久久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ù)指針?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

在C++中實(shí)現(xiàn)回調(diào)函數(shù)時(shí),我是否還應(yīng)該使用C風(fēng)格的函數(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;

推薦答案

簡(jiǎn)而言之,除非有理由不使用,否則請(qǐng)使用 std::function.

函數(shù)指針的缺點(diǎn)是不能捕獲某些上下文.例如,您將無(wú)法將 lambda 函數(shù)作為捕獲某些上下文變量的回調(diào)傳遞(但如果它不捕獲任何上下文變量,它將起作用).因此,調(diào)用對(duì)象的成員變量(即非靜態(tài))也是不可能的,因?yàn)樾枰东@對(duì)象(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 起)主要用于存儲(chǔ)一個(gè)函數(shù)(傳遞它不需要存儲(chǔ)它).因此,如果您想將回調(diào)存儲(chǔ)在例如成員變量中,這可能是您的最佳選擇.而且如果你不存儲(chǔ)它,它也是一個(gè)不錯(cuò)的首選"盡管它的缺點(diǎn)是在調(diào)用時(shí)引入了一些(非常小的)開(kāi)銷(xiāo)(因此在非常關(guān)鍵的性能情況下它可能是一個(gè)問(wèn)題,但在大多數(shù)情況下它不應(yīng)該).它非常通用":如果您非常關(guān)心一致和可讀的代碼,并且不想考慮您所做的每一個(gè)選擇(即想要保持簡(jiǎn)單),請(qǐng)使用 std::function 對(duì)于您傳遞的每個(gè)函數(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.

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

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ù)的版本示例(對(duì)于 pre-C++11,編寫(xiě) & 而不是 &&):

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)缺點(diǎn):


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

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


(1) 存在克服此限制的解決方法,例如將附加數(shù)據(jù)作為進(jìn)一步的參數(shù)傳遞給您的(外部)函數(shù):myFunction(..., callback, data) 將調(diào)用 callback(data).這是 C 風(fēng)格的帶參數(shù)回調(diào)",這在 C++ 中是可能的(順便說(shuō)一下,在 WIN32 API 中大量使用)但應(yīng)該避免,因?yàn)槲覀冊(cè)?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ù)摰氖穷?lèi)模板,即存儲(chǔ)函數(shù)的類(lèi)是模板.但這意味著在客戶(hù)端,函數(shù)的類(lèi)型決定了存儲(chǔ)回調(diào)的對(duì)象的類(lèi)型,這在實(shí)際用例中幾乎從來(lái)不是一個(gè)選項(xiàng).

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

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

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(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++ 中,為什么我不能像這樣編寫(xiě) 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++ 中重用線(xiàn)程)
Precise thread sleep needed. Max 1ms error(需要精確的線(xiàn)程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 二区在线视频 | 一区二区三区中文字幕 | 激情欧美一区二区三区 | 一区二区三区成人 | 中文字幕视频在线免费 | 午夜视频在线观看网址 | 国产精品69毛片高清亚洲 | 成人免费看 | 久草新在线| 亚洲精品国产综合区久久久久久久 | 国产一二区免费视频 | 亚洲国产aⅴ成人精品无吗 亚洲精品久久久一区二区三区 | 玖玖在线精品 | 黄色一级电影在线观看 | 亚洲久久| 国产精品福利网站 | 超碰男人天堂 | 精品自拍视频 | jvid精品资源在线观看 | 欧美日韩精品免费观看 | 中文字幕在线免费观看 | 影音先锋中文字幕在线观看 | 成年人网站在线观看视频 | 亚洲精品一二三 | 欧美一区二区三区在线观看视频 | 人妖videosex高潮另类 | 精品国产一区久久 | 婷婷91 | 在线播放国产一区二区三区 | 国产美女一区二区 | 成人免费视频在线观看 | 国产精品免费视频一区 | 夜夜草 | 草久久 | 在线观看av网站 | 欧美一级免费黄色片 | 成人国产一区二区三区精品麻豆 | 精精国产xxxx视频在线播放 | 97视频在线观看网站 | 成人精品一区二区 | 日本精品一区二区三区视频 |