3 回答

TA貢獻1828條經驗 獲得超3個贊
你可以用static_cast<>()指定哪個f根據函數指針類型所隱含的函數簽名使用:
// Uses the void f(char c); overload
std::for_each(s.begin(), s.end(), static_cast<void (*)(char)>(&f));
// Uses the void f(int i); overload
std::for_each(s.begin(), s.end(), static_cast<void (*)(int)>(&f));
或者,你也可以這樣做:
// The compiler will figure out which f to use according to
// the function pointer declaration.
void (*fpc)(char) = &f;
std::for_each(s.begin(), s.end(), fpc); // Uses the void f(char c); overload
void (*fpi)(int) = &f;
std::for_each(s.begin(), s.end(), fpi); // Uses the void f(int i); overload
f
mem_fun
- 3 回答
- 0 關注
- 454 瀏覽
添加回答
舉報