I found this example when I ran PCLint in the source code of the project I'm working on. Here is the code snippet:
QString foo()
{
return false;
}
I compiled the code and verified that it returns an empty string. A real example is a big method in a large class, and somewhere in some remote if branch, this is an isolated
return false;
Well, this is bad coding, a shame for the developer (using SVN / guilt, I could even find out who did it :-)), but seriously, why does the compiler not complain?
My theory is that the compiler translates
return false;
to
return QString(((const char *) false));
However, I do not see all the elementary steps that the compiler takes to do this. First he tries all the QString constructors and finds
QString(const * char);
? , bool const char *. bool , bool, ?
. ( "return false", " "?), (, ), , , ? -Wall g++ .
. , , .
return true;
:
error: conversion from βboolβ to non-scalar type βQStringβ requested
g++ 4.4.3 Ubuntu. , .