samedi 21 décembre 2019

Should we check a function not empty before applied with std::bind?

std::function<void(bool)> f;
std::function<void()> binded_f = std::bind(f, true);
std::cout << (f != nullptr) << " " << (binded_f != nullptr) << "\n";
f(true);
binded_f();

Above code gave output 0 1, and binded_f() crash with Unhandled exception at 0x00007FFE7B63A388 in: Microsoft C++ exception: std::bad_function_call at memory location 0x00000096960FA660. occurred in MSVC.

Seems calling the null function f is fine, while after std::bind applied, it will crash. What should we do? Do we need check a function before binded?

Aucun commentaire:

Enregistrer un commentaire