问题
Why can't I write the following code?
#include <fstream>
#include <string>
bool touch(const std::string& file_path)
{
return std::ofstream(file_path, std::ios_base::app);
}
int main()
{
touch("foo.txt");
}
Output
prog.cpp: In function 'bool touch(const string&)':
prog.cpp:6:52: error: cannot convert 'std::ofstream {aka std::basic_ofstream<char>}' to 'bool' in return
return std::ofstream(file_path, std::ios_base::app);
http://ideone.com/IhaRaD
I know that std::fstream
's operator bool()
defined as explicit
but I don't see any reason why it should fail in such case. There's no intermediate conversion, just the temporary std::ofstream
object and bool
. What's the reason?
回答1:
It's exactly because operator bool()
is defined as explicit
that you can't use it in this way. The only context where an explicit operator bool()
is automatically invoked is for unambiguous conditionals, such as if
while
, ?:
, !
and the middle expression of for
. (For a more complete summary, see my question When can I use explicit operator bool without a cast?).
A return
statement's value is never contextually converted to bool
, so if you want to convert std::ofstream
to bool
as a return value, you must use static_cast<bool>()
or equivalent.
回答2:
As the operator is declared like explicit and there is no context that allows implicit converting to bool (as for example using in the if statement) then you have to convert the expression with the stream to bool
explicitly.
For example
bool touch(const std::string& file_path)
{
return bool( std::ofstream(file_path, std::ios_base::app) );
}
回答3:
The definition of operator bool
looks like this:
explicit operator bool() {/*...*/}
Note the use of explicit here, this means there is no auto casting from class to bool. That means for your code, you have to 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");
}
No matter what, a cast is required (preferably static_cast<bool>
), because of implicit conversion being dangerous.
来源:https://stackoverflow.com/questions/39290725/why-cant-i-use-operator-bool-for-stdofstream