The following code is supposed to remove the last char of a string and append l
(lowercase L) if flip
is true or r
if it's false.
std::stringstream ss;
ss << code.substr(0, code.size() - 1);
ss << flip ? "l" : "r";
std::string _code = ss.str();
However, when flip
is true, it appends 1
and when it's false, it appends 0
. How come?
Precedence issue.
ss << flip ? "l" : "r";
means
(ss << flip) ? "l" : "r";
Use
ss << ( flip ? "l" : "r" );
It has to do with operator precedence.
<<
has priority over ?
which means flip
is appended onto ss
first.
The following should lead to the expected behaviour:
ss << (flip ? "l" : "r");
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