如何使用C++獲取指定的重載函數(shù)地址
剛剛看到一篇博客,說(shuō) std::bind 無(wú)法綁定正確的重載函數(shù)。這里的問(wèn)題并不是 std::bind 能力不足,而是將函數(shù)名傳遞給 std::bind 時(shí)編譯器無(wú)法取到這個(gè)函數(shù)的地址(也就是符號(hào),編譯器會(huì)先解析成符號(hào),鏈接器再替換為地址),因?yàn)橛卸鄠€(gè)重載函數(shù)都是這個(gè)名字。核心問(wèn)題是無(wú)法通過(guò)函數(shù)名取到想要的重載函數(shù)地址。就像下面的代碼無(wú)法編譯通過(guò):
#include <iostream> void f() { std::cout << "f 1" << std::endl; } void f(int x) { std::cout << "f 2 " << x << std::endl; } int main() { auto p = &f; }
編譯錯(cuò)誤:
/home/abc/cpplearn/overload_func.cpp: In function ‘int main()’:
/home/abc/cpplearn/overload_func.cpp:15:15: error: unable to deduce ‘auto’ from ‘& f’
15 | auto p = &f;
| ^
/home/abc/cpplearn/overload_func.cpp:15:15: note: couldn’t deduce template parameter ‘auto’
有沒(méi)有什么比較完美的解決辦法呢?我覺(jué)得一定有,因?yàn)?C 語(yǔ)言沒(méi)有函數(shù)重載,函數(shù)地址作為實(shí)參也是常規(guī)操作。相比之下,C++ 引入了函數(shù)重載,卻無(wú)法取到函數(shù)地址,這就很尷尬。C++ 設(shè)計(jì)者肯定也想到了這個(gè)問(wèn)題。
于是查閱了 cppreference.com,看到了 Address of an overloaded function。函數(shù)名的重載解析除了發(fā)生在函數(shù)調(diào)用的時(shí)候,也會(huì)發(fā)生在以下 7 種語(yǔ)境:
# | Context | Target |
---|---|---|
1 | initializer in a declaration of an object or reference | the object or reference being initialized |
2 | on the right-hand-side of an assignment expression | the left-hand side of the assignment |
3 | as a function call argument | the function parameter |
4 | as a user-defined operator argument | the operator parameter |
5 | the return statement | the return type of a function |
6 | explicit cast or static_cast argument | the target type of a cast |
7 | non-type template argument | the type of the template parameter |
當(dāng)函數(shù)名存在于這 7 種語(yǔ)境時(shí),會(huì)發(fā)生重載解析,并且會(huì)選擇與 Target 類型匹配的那個(gè)重載函數(shù)。這里就不一一考察這 7 種語(yǔ)境了,有興趣可以自己查閱 cppreference.com。這里重點(diǎn)考察第 3 種和第 6 種。
先看第 3 種語(yǔ)境。當(dāng)函數(shù)名作為函數(shù)調(diào)用的實(shí)參時(shí),重載解析會(huì)選擇和形參類型相匹配的版本。也就是說(shuō),下面的代碼會(huì)如期運(yùn)行:
#include <iostream> void f() { std::cout << "f 1" << std::endl; } void f(int x) { std::cout << "f 2 " << x << std::endl; } void call(void p(int)) { p(1); } int main() { call(f); }
這段代碼輸出:
f 2 1
回到最初的問(wèn)題,std::bind 也是函數(shù),為什么無(wú)法正常編譯呢?直接分析下面代碼的編譯錯(cuò)誤信息:
#include <iostream> #include <functional> void f() { std::cout << "f 1" << std::endl; } void f(int x) { std::cout << "f 2 " << x << std::endl; } int main() { auto new_func = std::bind(f, std::placeholders::_1); new_func(66); }
編譯錯(cuò)誤:
/home/abc/cpplearn/overload_func.cpp: In function ‘int main()’:
/home/abc/cpplearn/overload_func.cpp:16:30: error: no matching function for call to ‘bind(<unresolved overloaded function type>, const std::_Placeholder<1>&)’
16 | auto new_func = std::bind(f, std::placeholders::_1);
| ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
可以看到,std::bind 準(zhǔn)確地說(shuō)是一個(gè)函數(shù)模板。它要根據(jù)其參數(shù)進(jìn)行模板實(shí)參推導(dǎo),再替換模板形參進(jìn)行實(shí)例化(Instantiation),產(chǎn)生和普通函數(shù)類似的匯編代碼。std::bind 進(jìn)行實(shí)例化的時(shí)候,函數(shù) f
還沒(méi)有進(jìn)行重載解析,其類型為<unresolved overloaded function type>
。std::bind 無(wú)法進(jìn)行實(shí)例化。怎樣修改可以解決這個(gè)問(wèn)題呢?
可以利用第 6 個(gè)語(yǔ)境,也就是顯示轉(zhuǎn)換或 static_cast。重載解析會(huì)選擇與它們的目標(biāo)類型相匹配的版本。下面的代碼會(huì)如期運(yùn)行:
#include <iostream> #include <functional> void f() { std::cout << "f 1" << std::endl; } void f(int x) { std::cout << "f 2 " << x << std::endl; } int main() { auto new_func = std::bind((void(*)(int))f, std::placeholders::_1); new_func(66); }
這段代碼輸出:
f 2 66
還有一種更加巧妙的辦法,依然是利用第 3 種語(yǔ)境。既然 std::bind 的第一個(gè)模板實(shí)參的推導(dǎo),和 f 的重載解析相矛盾。為什么不直接解決這個(gè)矛盾,將第一個(gè)模板實(shí)參改為顯示指定?來(lái)看下面的代碼:
#include <iostream> #include <functional> void f() { std::cout << "f 1" << std::endl; } void f(int x) { std::cout << "f 2 " << x << std::endl; } int main() { auto new_func = std::bind<void(int)>(f, std::placeholders::_1); new_func(66); }
這段代碼如期輸出:
f 2 66
總結(jié)
到此這篇關(guān)于如何使用C++獲取指定的重載函數(shù)地址的文章就介紹到這了,更多相關(guān)C++獲取重載函數(shù)地址內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解C++ 拷貝構(gòu)造函數(shù)和賦值運(yùn)算符
本文主要介紹了拷貝構(gòu)造函數(shù)和賦值運(yùn)算符的區(qū)別,以及在什么時(shí)候調(diào)用拷貝構(gòu)造函數(shù)、什么情況下調(diào)用賦值運(yùn)算符。最后,簡(jiǎn)單的分析了下深拷貝和淺拷貝的問(wèn)題。有需要的朋友可以看下2016-12-12C++11新特性之四種類型轉(zhuǎn)換cast說(shuō)明
類型轉(zhuǎn)換是項(xiàng)目中常使用的一種語(yǔ)法規(guī)則,幾乎每個(gè)編程語(yǔ)言都不可避免的涉及到這方面,下面這篇文章主要給大家介紹了關(guān)于C++11新特性之四種類型轉(zhuǎn)換cast說(shuō)明的相關(guān)資料,需要的朋友可以參考下2023-02-02C++指針和數(shù)組:字符和字符串、字符數(shù)組的關(guān)聯(lián)和區(qū)別
字符串是一種重要的數(shù)據(jù)類型,但是c語(yǔ)言并沒(méi)有顯示的字符串?dāng)?shù)據(jù)類型,因?yàn)樽址宰址A康男问匠霈F(xiàn)或者存儲(chǔ)于字符數(shù)組中。在C++標(biāo)準(zhǔn)模板庫(kù)(STL)中提供了string類,實(shí)現(xiàn)了對(duì)字符串的封裝。2022-12-12C++中整形與浮點(diǎn)型如何在內(nèi)存中的存儲(chǔ)詳解
大家好!這期和大家分享整形和浮點(diǎn)型是如何在數(shù)據(jù)是如何在內(nèi)存中存儲(chǔ),下面文章具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05C語(yǔ)言字母轉(zhuǎn)換大小寫(xiě)的3種方法圖文詳解
我們?cè)贑語(yǔ)言入門(mén)的時(shí)候都會(huì)遇到要求寫(xiě)大小寫(xiě)轉(zhuǎn)換的題目,所以下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言字母轉(zhuǎn)換大小寫(xiě)的3種方法,文中給了詳細(xì)的代碼示例,需要的朋友可以參考下2023-10-10