如何使用C++獲取指定的重載函數(shù)地址
剛剛看到一篇博客,說 std::bind 無法綁定正確的重載函數(shù)。這里的問題并不是 std::bind 能力不足,而是將函數(shù)名傳遞給 std::bind 時編譯器無法取到這個函數(shù)的地址(也就是符號,編譯器會先解析成符號,鏈接器再替換為地址),因為有多個重載函數(shù)都是這個名字。核心問題是無法通過函數(shù)名取到想要的重載函數(shù)地址。就像下面的代碼無法編譯通過:
#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; }
編譯錯誤:
/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’
有沒有什么比較完美的解決辦法呢?我覺得一定有,因為 C 語言沒有函數(shù)重載,函數(shù)地址作為實參也是常規(guī)操作。相比之下,C++ 引入了函數(shù)重載,卻無法取到函數(shù)地址,這就很尷尬。C++ 設(shè)計者肯定也想到了這個問題。
于是查閱了 cppreference.com,看到了 Address of an overloaded function。函數(shù)名的重載解析除了發(fā)生在函數(shù)調(diào)用的時候,也會發(fā)生在以下 7 種語境:
# | 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 |
當函數(shù)名存在于這 7 種語境時,會發(fā)生重載解析,并且會選擇與 Target 類型匹配的那個重載函數(shù)。這里就不一一考察這 7 種語境了,有興趣可以自己查閱 cppreference.com。這里重點考察第 3 種和第 6 種。
先看第 3 種語境。當函數(shù)名作為函數(shù)調(diào)用的實參時,重載解析會選擇和形參類型相匹配的版本。也就是說,下面的代碼會如期運行:
#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
回到最初的問題,std::bind 也是函數(shù),為什么無法正常編譯呢?直接分析下面代碼的編譯錯誤信息:
#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); }
編譯錯誤:
/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 準確地說是一個函數(shù)模板。它要根據(jù)其參數(shù)進行模板實參推導(dǎo),再替換模板形參進行實例化(Instantiation),產(chǎn)生和普通函數(shù)類似的匯編代碼。std::bind 進行實例化的時候,函數(shù) f
還沒有進行重載解析,其類型為<unresolved overloaded function type>
。std::bind 無法進行實例化。怎樣修改可以解決這個問題呢?
可以利用第 6 個語境,也就是顯示轉(zhuǎn)換或 static_cast。重載解析會選擇與它們的目標類型相匹配的版本。下面的代碼會如期運行:
#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 種語境。既然 std::bind 的第一個模板實參的推導(dǎo),和 f 的重載解析相矛盾。為什么不直接解決這個矛盾,將第一個模板實參改為顯示指定?來看下面的代碼:
#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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++指針和數(shù)組:字符和字符串、字符數(shù)組的關(guān)聯(lián)和區(qū)別
字符串是一種重要的數(shù)據(jù)類型,但是c語言并沒有顯示的字符串數(shù)據(jù)類型,因為字符串以字符串常量的形式出現(xiàn)或者存儲于字符數(shù)組中。在C++標準模板庫(STL)中提供了string類,實現(xiàn)了對字符串的封裝。2022-12-12