問(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ù)ptr | std::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)論) | no | no | 是 |
可以存放在一個(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)!