The definition of operator bool as follows:
explicit operator bool() {}
Note the use of explicit here, this means that there is no automatic cast from class to bool. This means that for your code you must do this:
#include <fstream> #include <string> bool touch(const std::string& file_path) { return static_cast<bool>(std::ofstream(file_path, std::ios_base::app)); } int main() { touch("foo.txt"); }
Regardless of what casting is required (preferably static_cast<bool> ), due to the implicit conversion being dangerous.
source share