|
|
|
|
|
|
|
|
std::cout << "1DEBUG error"
doesn't output either.
|
|
|
|
|
|
seq
and then just avoid operator overloading overall?
|
|
Also if the problem is in the loop, why doesn't this work? : |
|
|
Makes debugging my error that much harder... Q_Q |
|
|
<=
etc. At the moment it is interpreted like this :if (!(0) <= index || !(index) < aLength)
!0
is pretty insidious because it equals in binary 1111... and !index
will be bad for the same reasons, any way suffice to say that neither of those values will represent the boundaries of the array.
|
|
nullptr
from here - it's expecting a reference so that might be OK. Obviously, one needs to test for that - which is a bit awkward if in the middle of a larger expression.static_assert
which produces a compile error (with your own error message) when the statement evaluates to false. But this is a compile time thing and is probably only really useful when you test with hard coded values, so it is not really providing a run time solution.http://en.cppreference.com/w/cpp/language/operator_precedence http://en.cppreference.com/w/cpp/language/static_assert |
false
or true
.index
is 0 Array Length must be positive :
|
|
index
> 0, or negative :
|
|
0<= index
q index < arraySize
|
|
index < 0
index > arraySize
greater thanp && q
is wrong as well, it can't be both.if (index < 0 || index > ArraySize) {/* error exists */}
while(!Quit) {}
, I mean just for anything more than trivial where there is potential for confusion.I only did that because Eclipse has a stupid bug that gives warning if your return statement is in a condition. |
p is wrong : zero is a valid index - it should be index < 0 q is wrong , it should be index > arraySize greater than p && q is wrong as well, it can't be both. |
index < 0
or index > arraySize
are applying the negation of my statement which is what I did by just adding !()
.0<= index
isn't valid aka false. Then you say that index < 0
is correct aka true , which is just the negation of my statement so . If you check my previous post, I made it clear that the alternative of my statement was yours. index < 0
|| !q is index > arraySize
Nonono you misunderstood the intention of my control statement. I'm trying to detect if the array being accessed is valid |
std::cerr << "Accessing non valid index";
|
|
|
|
!(0 <= index)
is not the same as (index < 0)
!(0 <= index)
!(0 <= true)
!(0 <= 1)
!(true)
false
!(0 <= index)
!(0 <= 0)
!(true)
false
!(index < aLength)
, that works logically because if the first part was correct and catches a negative value, the whole expression is short circuited (the first part was true, so that is all that is evaluated in the or expression). But the first part isn't at the moment correct either.if ( !(index < aLength) || !(0 <= index) )
I prefer thinking of negation as statements and simply adding NOT, the idea of switching the actual statements bothers me. Although I do see positions where doing that will make the code uglier or unsmooth. |
If index is positive or negative, the result is the same: [The main reason being that positive and negative values both return true, and you have a different relational operator .] !(0 <= index) !(0 <= true) !(0 <= 1) !(true) FINAL RESULT: [false] <--- POSITIVE EXAMPLE. |
!(0 <= index) !(0 <= -1) !( 0 <= -1) !(false) FINAL RESULT: [true] <----NEGATIVE EXAMPLE. RESULT IS NOT THE SAME |
However observe what happens if one reverses those 2 expressions: if ( !(index < aLength) || !(0 <= index)) The first one fails for negative values of index, as does the whole expression. |
if ( !(index < aLength) || !(0 <= index) )
And let me show that !(0 <= index) is not the same as (index < 0) |
I think you have some kind of misunderstanding. I'm not just applying these negations randomly, I'm using Discrete Mathematics Proposition functions. |
PI * r * r
, pretty much everyone would immediately see that as being correct; But if someone else was to say that it was also equal to PI * (D * D) / 4
(D is Diameter), then there would be quite a few who have to think about that a little to verify it was correct. Which it is, but I would wager that most would prefer the first one.
|
|