c++ - Logical AND operator in diab 5.7 compiler returns non-bool type -


when following bit of code compiled diab c++ compiler (dplusplus), generates conversion warning on third line. can resolved casting result of (&&) operator other bool.

code:

bool first = 1; bool second = 1; bool ret = (first && second); //generates compile warning 

error:

warning: (etoa:1643): narrowing or signed-to-unsigned type conversion found: int unsigned char

i verified nothing defining bool type. compiler issue, or there else might missing cause this?

wind river's web site indicates diab compiler can compile either c or c++.

in c, && operator yields result of type int, value 0 or 1. that's consistent warning you're seeing.

as of 1990 iso standard, c did not have built-in bool type. common define bool typedef. appears message bool typedef unsigned char, in header. 1999 iso c standard adds new predefined boolean type called _bool; identifier bool defined in <stdbool.h> macro expands _bool. if <stdbool.h> isn't included, bool can defined in other way.

in c++, && yields result of type bool value false or true, , bool distinct fundamental type. has been case @ least since 1998 iso c++ standard.

i suspect you're getting warning because you're compiling code c rather c++. less possibility diab compiler doesn't conform c++ standard; might have way tell conform more closely.

i haven't used diab compiler. typically can control language being compiled using particular file extension (typically .c c , .cpp c++), or using different command, or both.

consult compiler's documentation find out how invoke conforming c++ compiler.

as experiment, before changing way invoke compiler, might try adding declaration:

int class; 

to source file. legal in c, syntax error in c++ (since class c++ keyword).

update:

the op says he's compiling c++, not c. warning message implies && yields int, , bool same type unsigned char.

a warning doesn't directly indicate compiler not conforming; compilers can warn like. content of warning suggest compiler bug, or @ least compiler doesn't conform c+

any conforming c++ compiler must produce diagnostics program. compiler do? (please don't add #include directives.)

int main() {     class dummy { };         // make sure it's c++     bool b;     unsigned char c;     bool* pb = &c;           // invalid conversion     unsigned char* pc = &b;  // invalid conversion } 

and output when compile , execute program?

#include <iostream> int main() {     std::cout << "__cplusplus = " << __cplusplus << "\n"; } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -