2013-07-30 29 views
5

假設你有2個免費功能:如何拋出重載的自由函數來解決過載衝突?

void do_something(dog d); 
void do_something(cat c); 

沒有說你要這些功能傳遞給模板函數:

template <typename DoSomethingFunc> 
void do_something_template(DoSomethingFunc func); 

什麼是首選的方法來調用do_something_template以避免方式重載解決衝突?它會鑄造嗎?

+0

do_something_template做什麼?我懷疑它需要一個動物論證,它應該選擇和調用這個動物的適當功能? –

回答

5

你可以施放或使用本地函數指針變量。

void (*p)(dog) = do_something; 
do_something_template(p); 

do_something_template(static_cast<void(*)(cat)>(do_something)); 
0

是的,只投它:

struct dog {}; 
struct cat {}; 

void do_something(dog d); 
void do_something(cat c); 

template <typename DoSomethingFunc> 
void do_something_template(DoSomethingFunc func); 

void f() { 
    do_something_template((void(*)(dog))do_something); 
    do_something_template(static_cast<void(*)(cat)>(do_something)); 
} 
0

可以使用C++蒙上投它 - 在這種情況下static_cast是最合適的。

do_something_template(
    static_cast<void(*)(dog)>(do_something) 
);