Quantcast
Channel: Can (a==1)&&(a==2)&&(a==3) evaluate to true? (and can it be useful?) - Stack Overflow
Browsing all 9 articles
Browse latest View live

Answer by M.M for Can (a==1)&&(a==2)&&(a==3) evaluate to true? (and can it be...

Other things not mentioned yet (source):a might have overloaded operator int(), the operator for implicit conversion to int (instead of operator== as covered by other answers).a might be a preprocessor...

View Article



Answer by Stanislav for Can (a==1)&&(a==2)&&(a==3) evaluate to true? (and can...

As it was noticed before, this trick could be performed with volatile. This is more honest approach compared to operator changing. Just let us use two threads:volatile int a;void changingValue(){...

View Article

Answer by mathreadler for Can (a==1)&&(a==2)&&(a==3) evaluate to true? (and...

Just to show my own ideas. I was thinking of some data structure like a stream buffer or a ring buffer.We can with template inheritance "hide away the operator" and nothing will be altered in the data...

View Article

Answer by besc for Can (a==1)&&(a==2)&&(a==3) evaluate to true? (and can it...

I assume a requirement is a valid program free of undefined behaviour. Otherwise simply introduce something like a data race and wait for the right circumstances to occur.In a nutshell: Yes, it is...

View Article

Answer by Christian Hackl for Can (a==1)&&(a==2)&&(a==3) evaluate to true?...

Operator overloading and macros are trivial solutions to such a riddle.And if so, can it actually be useful?Well, with some imagination... One possible use case I can think of are unit tests or...

View Article


Answer by super for Can (a==1)&&(a==2)&&(a==3) evaluate to true? (and can it...

Could be somewhat useful.#include <iostream>#include <algorithm>#include <vector>using namespace std;struct Foo { std::vector<int> v = {1,2,3};};bool operator==(const Foo&...

View Article

Answer by Edgar Rokjān for Can (a==1)&&(a==2)&&(a==3) evaluate to true? (and...

Can the expression evaluate to true in C++?Yes, nothing is impossible...struct A { int x = 0;};bool operator==(A& a, int num) { return ++a.x == num;}Then:if ((a == 1) && (a == 2) &&...

View Article

Answer by Hatted Rooster for Can (a==1)&&(a==2)&&(a==3) evaluate to true?...

Yes it can:class Foo{ public: bool operator==(int a) { return true; }};Then, let a be of type Foo and voila.Can this actually be useful? I don't really see it being useful no.

View Article


Can (a==1)&&(a==2)&&(a==3) evaluate to true? (and can it be useful?)

Inspired by another question regarding java-script language. Can the expression (a==1)&&(a==2)&&(a==3)evaluate to true in C++? (And if so, can it actually be useful?)

View Article

Browsing all 9 articles
Browse latest View live




Latest Images