2012-08-28 31 views
0

我想定義一個特定的函數指針類型,用於我的boost::bind調用中,以解決與函數重載無法識別相關的問題(通過調用static_cast)。我正在明確定義typedef以解決std::string::compare上的含糊問題。std :: string函數指針的typedef不起作用

當我寫這個函數時,我得到了錯誤。

typedef int(std::string* resolve_type)(const char*)const; 

你知道這個typedef有什麼問題嗎?

回答

4

我想你想要這個。

typedef int(std::string::*resolve_type)(const char*) const; 

例子。

#include <iostream> 
#include <functional> 

typedef int(std::string::*resolve_type)(const char*)const; 

int main() 
{ 
    resolve_type resolver = &std::string::compare; 
    std::string s = "hello"; 
    std::cout << (s.*resolver)("hello") << std::endl; 
} 

http://liveworkspace.org/code/4971076ed8ee19f2fdcabfc04f4883f8

並與綁定例如

#include <iostream> 
#include <functional> 

typedef int(std::string::*resolve_type)(const char*)const; 

int main() 
{ 
    resolve_type resolver = &std::string::compare; 
    std::string s = "hello"; 
    auto f = std::bind(resolver, s, std::placeholders::_1); 
    std::cout << f("hello") << std::endl; 
} 

http://liveworkspace.org/code/ff1168db42ff5b45042a0675d59769c0

+0

嗨永遠。非常感謝(有些延遲),因爲它運行良好,我沒有意識到有關失蹤::。 –