3

I have the following check in my C++ code:

if (not obj.someBoolean) {
      // some code
} else {
      // some other code
}   

A print statement or gdb confirms that obj.someBoolean (a bool variable) is false.
Yet the control goes to the else block while using not operator.

Interestingly, the ! variety of the operator works correctly when used in the above scenario (goes into the if block).

Is this an issue with the way I am using not?

Update (some more details on the scenario):

Throughout the code I have used not in many places. But this is one scenario where this issue comes up (consistently).

Even the following code works (goes to if block):

bool temp = not obj.someBoolean;
if (temp) {
      // some code
} else {
      // some other code
}     

This is more like a single random point where it is happening.
I was curious as to why this behavior is caused.

eternalthinker
  • 532
  • 6
  • 16
  • 2
    What compiler and which version of it are you using? – Some programmer dude Nov 07 '12 at 12:13
  • It's the first time I see `not` and I can't find any references (googling "not" is hard...) Why would you use it if you have `!`? – leemes Nov 07 '12 at 12:13
  • @leemes same. But I found this: http://lists.kde.org/?l=kdevelop-bugs&m=123565240514682 – Lews Therin Nov 07 '12 at 12:14
  • @Fixxxer yes it is. See [here](http://en.cppreference.com/w/cpp/keyword/not). – juanchopanza Nov 07 '12 at 12:14
  • 2
    @Fixxxer It's indeed an [alternative operator](http://en.cppreference.com/w/cpp/language/operator_alternative) keyword in C++. Otherwise the OP would get a compilation error instead of apparently wrong code generated. – Some programmer dude Nov 07 '12 at 12:15
  • My guess is that the operator precedence isn't equal for `not` vs. `!`. Try to enclose the expression to be negated in parentheses. However, I think that negating `obj` would lead to compiler errors... – leemes Nov 07 '12 at 12:17
  • @Fixxxer I assume you didn't see [this question](http://stackoverflow.com/questions/2376448/the-written-versions-of-the-logical-operators) then... – Eitan T Nov 07 '12 at 12:17
  • 1
    @eternalthinker Are you sure you don't have a macro `not` defined somewhere? – jrok Nov 07 '12 at 12:23
  • 3
    Could you provide a small code sample that compiles and reproduces the problem? – juanchopanza Nov 07 '12 at 12:23
  • I am using gcc version 4.1.2. There is no macro named 'not'. Also, the same operator is used in other places (See the updated portion of the question). – eternalthinker Nov 07 '12 at 14:55
  • @eternalthinker: did you try inspecting the disassembly of the code section ? If you manage to isolate it in a single function it should be "relatively" possible. – Matthieu M. Nov 07 '12 at 16:52

2 Answers2

2

I use not, and and or almost exclusively in C++ (I find them more readable and less error prone than their sigils counterparts). They are strictly equivalent.

§2.6 Alternative tokens [lex.digraph]

1/ Alternative token representations are provided for some operators and punctuators.

2/ In all respects of the language, each alternative token behaves the same, respectively, as its primary token, except for its spelling. The set of alternative tokens is defined in Table 2.

Look elsewhere.

Community
  • 1
  • 1
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
1

I don't see what's wrong with your code. Try this:

#include <iostream>

int main()
{
  if(not false) std::cout<< "true!"; 
}

If it prints "true!", the problem is somewhere else.

nurettin
  • 11,090
  • 5
  • 65
  • 85