我使用boost ::功能是這樣的:的boost ::綁定的隱式轉換提振::函數或函數指針
template<class T1>
void run(boost::function<void (T1)> func, string arg)
{
T1 p1 = parse<T1>(arg);
func(p1);
}
當這樣使用,一切正常:
void test1(int i)
{
cout << "test1 i=" << i << endl;
}
...
boost::function<void (int)> f = &test1;
run(f, "42");
我希望能夠將原始函數指針直接傳遞,所以我超載這樣的run()函數:
template<class T1>
void run(void (*func)(T1), string arg)
{
T1 p1 = parse<T1>(arg);
(*func)(p1);
}
...
run(&test1, "42"); // this is OK now
現在,我想能夠將boost :: bind的結果傳遞給run()函數。就像這樣:
void test2(int i, string s)
{
cout << "test2 i=" << i << " s=" << s << endl;
}
...
run(boost::bind(&test2, _1, "test"), "42"); // Edit: Added missing parameter 42
但是這不會編譯:編輯
bind.cpp: In function ‘int main()’:
bind.cpp:33:59: error: no matching function for call to ‘run(boost::_bi::bind_t<void, void (*)(int, std::basic_string<char>), boost::_bi::list2<boost::arg<1>, boost::_bi::value<std::basic_string<char> > > >, std::string)’
bind.cpp:33:59: note: candidates are:
bind.cpp:7:6: note: template<class T1> void run(boost::function<void(T1)>, std::string)
bind.cpp:14:6: note: template<class T1> void run(void (*)(T1), std::string)
我應該如何運行超載()接受的boost :: bind()的?
編輯2
我知道我能做到這一點是這樣的:
boost::function<void (int)> f = boost::bind(&test2, _1, string("test"));
run(f, "42");
但我想使用是更簡潔。
編輯3
更改的run()的原型來自run(boost::function<void (T1)>, T1)
到run(boost::function<void (T1)>, string)
闡述實際使用情況。參考。伊戈爾R.的回答
整個源文件可以得到here
沒有「隱式投射」這樣的事情。強制轉換是您在源代碼中編寫的內容,以告知編譯器進行轉換。編譯器可以在沒有轉換的情況下進行轉換;這種轉換是「隱式轉換」。當你使用強制轉換時,它是一個「顯式轉換」。 – 2013-04-09 10:03:10
確實。問題標題應該改變。 – anorm 2013-04-09 11:05:42