Because the problem with references was only a one example of how scopeguard could be used. But it should work (and work correctly) in all other cases. So having this clean up function:
void my_exit(const std::string & msg)
{
std::cout << "my_exit: " << msg << std::endl;
}
This should work (passing temporary object):
void test()
{
std::string msg("test_1 Hello World");
ON_BLOCK_EXIT(&my_exit, msg.substr(0, 6));
}
This should work (passing a reference to an object that will be destroyed before the scopeguard is called):
void test()
{
std::map<int, std::string> m;
m[42] = "test_2";
ON_BLOCK_EXIT(my_exit, m[42]);
m.clear();
}
And this should work (passing a const reference):
void test(const std::string & msg)
{
ON_BLOCK_EXIT(&my_exit, msg);
}
If the parm
was const or non-const reference then those examples would either not compile or crashed when run.