Why casting double to bool doesn't return an meaningful warning.

Why casting double to bool doesn't return an meaningful warning.

A simple program like this:

void func1(const std::string &name,
const std::string &name2,
double d1, bool use1 = true,
bool refc = false,
double d3 = -1,
bool check1=true){
std::cout <<"d1 "<
}

int main () {

double d1 = 0.134627;

func1("name1", "name2", d1, d1 );

return 0;
}

As you can see, the 4th parameter is implicitly casted to a bool. I wonder why it doesn't give a warning. I turned on all warnings, the only one says:
remark #1572: floating-point equality and inequality comparisons are unreliable

which actually not directly reflect this case.

Please help.

8 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

This example doesn't compile with g++ or icpc:
zw.cpp:2: error: expected unqualified-id before & token
zw.cpp:2: error: expected ) before & token
zw.cpp:2: error: expected initializer before & token
so it doesn't resemble C++ sufficiently to check your question on that basis.

I changed the code and it is given below:

// u66043.cpp

#include
using namespace std;

void func1(const string &name,
const string &name2,
double d1,
bool use1 = true,
bool refc = false,
double d3 = -1,
bool check1=true)
{
cout << "d1 (double) " << d1 << ", use1 (bool) " << use1 << ", d3 (double)" << d3 << ", check1 (bool) " << check1 << ", refc (bool) " << refc << endl;
//cout << name << endl;
//cout << name2 << endl;
}

int main () {

double d1 = 0.134627;

func1("name1", "name2", d1, 0.1 );

return 0;
}

I can compile the code with Intel C++ compiler in Windows. Implicit conversion from double to bool looks correct to me. If the value of double is zero then we get "fasle", printed as 0. If the value of double is non zero then we get "true", printed as 1.

You need to include header files. I only pasted a snip of code.

Thanks.

Sorry, that is not my point.

When I compile on Windows using VS compiler, I can get warning like:
warning C4800: 'double' : forcing value to bool 'true' or 'false' (performance warning)

But I don't get any warning from icc, either on Linux or Windows.

Quoting - Zhu Wang (Intel)
Sorry, that is not my point.

When I compile on Windows using VS compiler, I can get warning like:
warning C4800: 'double' : forcing value to bool 'true' or 'false' (performance warning)

But I don't get any warning from icc, either on Linux or Windows.

It's not a serious performance hit, unless you compile for x87 code, which you wouldn't do if you were interested in performance. Stylistic warnings are available in -Weffc++, but this is too off beat to be covered by those recommendations.

Quoting - Zhu Wang (Intel)
func1("name1", "name2", d1, d1 );
remark #1572: floating-point equality and inequality comparisons are unreliable

I agree that the msg could be improved, and will send a request to engineering team.

Thanks,
Jennifer

Thank you. Please let me know when it is adjusted.

Leave a Comment

Please sign in to add a comment. Not a member? Join today