I am currently converting some OpenCV code from C++ to Java. I can\'t use JavaCV, as we need the conversion in native Java, not a JNA. At one point in the code, I get the fo
The expression (kHit >= kForeground)
yields a boolean that has value true
or false
. When the unary -
is applied, the bool
gets promoted to an int
, and the conversion yields 1
for true
or 0
for false
. After the promotion, the sign is changed into -1
or 0
and then it is converted to uchar
by the outer cast.
Note that the important bit of information is that the unary operator-
is not applied to a boolean, but the boolean is converted to int
and it is then applied. That can be tested with a bit of template magic:
template
struct same_type {
static const bool value = false;
};
template
struct same_type {
static const bool value = true;
};
template
void f( T value ) {
std::cout << "Is int? " << std::boolalpha << same_type::value << "\n";
std::cout << "Is bool? " << same_type::value << "\n";
}
int main() {
f(-true);
}
The f
template tests the type of the passed argument against int
and bool
by using the same_type
templates above (trivial enough to understand). If we call the f
template with -true
as argument type deduction will set T
to be the type of the expression -true
. If you run the program, you will see that it prints Is int? true\nIs bool? false
.