Look at this little snippet.
y<v|!v?:y=v;
( y is the minimum value, and v is the current comparable value. This method will make you think easier.)
The meaning of this fragment is simple.
If the current value of v less than the minimum value of y , set a new minimum value ( y=v ). But the case v=0 excluded.
Then I thought that if a "bad code" could be generated, the result should be the same. I mean,
y>v&v?y=v:;
This code should do the same. But it can not be compiled. The following error.
error: expected expression for(int v: a) v=abs(a[i]-v), x>v?:x=v, y>v&v?y=v:; ^
It’s strange. I think the two codes are the same. If the last ternary operator is wrong, the former should have the same problem. But this is not so.
Can someone explain why?
The next question. I inserted 0 to compile. y>v&v?y=v:0;
Then I got a false answer. So I changed & to && . y>v&&v?y=v:0;
Finally, I got the correct answer. But without this process, the operator | can do everything. Why?
<additional information>
My compiler version is as follows.
$ gcc --version Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn) Target: x86_64-apple-darwin14.4.0 Thread model: posix
And the compilation option:
g++ -std=c++11 my.cpp
If you want to have sample code for testing, this will help.
#include <iostream>
(ps correcting my bad english is always welcome)