
244
|
6 章 ラムダ式
setAlarm(steady_clock::now() + 1h, // fine, calls
s, // 3-arg version
30s); // of setAlarm
};
実引数が 3 つの setAlarm
を呼び出す。問題なし
しかし std::bind ではコンパイルもできません。
auto setSoundB = // error! which
std::bind(setAlarm, // setAlarm?
std::bind(std::plus<>(),
エラー! どちらの
setAlarm か?
steady_clock::now(),
1h),
_1,
30s);
コンパイラにすれば、2 つある setAlarm 関数のどちらを std::bind に渡すべきかを判断できま
せん。コンパイラにとって既知なのは関数名だけであり、これだけでは情報不足で曖昧なのです。
上例の std::bind 呼び出しをコンパイル可能にするには、setAlarm を適切な関数ポインタ型へ
キャストする必要があります。
using SetAlarm3ParamType = void(*)(Time t, Sound s, Duration d)
;
auto setSoundB = // now
std::bind(static_cast<SetAlarm3ParamType>(setAlarm), // okay
std::bind(std::plus<>(), これで OK
steady_clock::now(), ...