Do I understand correctly that bool to int cast should cast true to 1?
GCC 4.8.1 gives strange result for this code:
#include <array>
#include <iostream>
int main()
{
using namespace std;
array<bool, 3> bb;
for ( int i = 0; i < 3; i++ ) cout << static_cast<int>( bb[i] ) << endl;
return 0;
}
Here is what I get:
>> g++ -std=c++11 test_bool.cpp -pedantic -O3
>> ./a.out
136
251
160
Do I understand correctly that
booltointcast should casttrueto1?
Yes.
GCC 4.8.1 gives strange result for this code:
That's because your program has undefined behavior, since your array is not initialized. Try, for instance, this:
array<bool, 3> bb = { true, false, true };
And you will see a consistent output. Here is a live example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With