Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

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

Zhu_W_Intel
Employee
1,559 Views
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 "<<<" "<<<" "<<<"check "<<< " ref "<<<" name "<<<" name2 "<
}

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.
0 Kudos
7 Replies
TimP
Honored Contributor III
1,559 Views
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.
0 Kudos
Om_S_Intel
Employee
1,559 Views

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.


0 Kudos
Zhu_W_Intel
Employee
1,559 Views

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

Thanks.
0 Kudos
Zhu_W_Intel
Employee
1,559 Views
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.
0 Kudos
TimP
Honored Contributor III
1,559 Views
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.
0 Kudos
JenniferJ
Moderator
1,559 Views
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
0 Kudos
Zhu_W_Intel
Employee
1,559 Views

Thank you. Please let me know when it is adjusted.
0 Kudos
Reply