Purpose of and() function in C++ [duplicate] - c++

This question already has answers here:
or is not valid C++ : why does this code compile?
(3 answers)
Closed 9 years ago.
What is the function of and() in C++ and its syntax?
P.S. I happened to write out and() as a function and the C++ text editor highlighted it.
Even after much searching I could not find its function or the syntax.

There is no and function in C++, it's a reserved identifier, the same as the logical operator &&.
C++11(ISO/IEC 14882:2011) §2.5 Alternative tokens
In C, there's no and keyword, but if you include the header iso646.h, and is the same as && as well.
C11(ISO/IEC 9899:201x) §7.9 Alternative spellings
The header <iso646.h> defines the following eleven macros (on the left) that expand
to the corresponding tokens (on the right):
and &&
and_eq &=
bitand &
bitor |
compl ~
not !
not_eq !=
or ||
or_eq |=
xor ^
xor_eq ^=

and is not a function; it is an operator. It means the same thing as &&. For example,
x && y
and
x and y
mean the same thing.
If you try to use it as a function, it will give you an error.
See this answer for more information on and, or, etc.

Related

Accidentally found "and" statement in C++ [duplicate]

Here is a very simple C++ application I made with QtCreator :
int main(int argc, char *argv[])
{
int a = 1;
int b = 2;
if (a < 1 or b > 3)
{
return 1;
}
return 0;
}
To me, this is not valid C++, as the keyword or is not a reserved keyword.
But if I compile and run it, it works fine without any warnings ! The exit code is 0 and if I change b = 4, the exit code is 1 !
I'm not including anything to make sure there is no hidden define.
This is really strange to me. Is this something Qt is defining ? I didn't find anything in the documentation regarding that.
According to Wikipedia:
C++ defines keywords to act as aliases
for a number of symbols that function
as operators: and (&&), bitand (&),
and_eq (&=), or (||), bitor (|), or_eq
(|=), xor (^), xor_eq (^=), not (!),
not_eq (!=), compl (~).
As MadKeithV points out, these replacements came from C's iso646.h, and were included in ISO C++ as operator keywords. The Wikipedia article for iso646.h says that the reason for these keywords was indeed for international and other non-QWERTY keyboards that might not have had easy access to the symbols.
or is a C++ keyword, and you're allowed to use it instead of ||. There is no magic.
The same goes for and and most other logical operators. It's generally best to stick to the commonly known names though, to avoid confusion like this. If you use or, someone will wonder "why does this compile" ;)
iso646.h defines a number of operator alternatives - it's part of the C++ standard.

How to use logical XOR operator in macro? NOT how to defined XOR operator by macro [duplicate]

This question already has answers here:
Why is there no ^^ operator in C/C++?
(7 answers)
Logical XOR operator in C++?
(11 answers)
Closed 6 years ago.
Is there any tricky way to use logical xor operator ^^ in macro in C, C++, Objective-C?
I have tried applying ^^ directly in Objective-C, it does not work.
Edited: let me clarify my answer.
What I want is to use xor operator in macro. It does not mean "how to define the xor operator by a macro.
I.e, I want something like
#if defined(x) ^^ TARGET_OS_IOS ^^ __cplusplus
For seconds after posting the question, I figured out the answer my self.
!(A) != !(B) will be equivalent to xor operator
A better solution in case the number of operands is different than 2
!(A) ^ !(B) ^ !(C) ^ ...

Can a MACRO be compared in #if with non numeric value [duplicate]

This question already has answers here:
How to compare strings in C conditional preprocessor-directives
(14 answers)
Closed 6 years ago.
consider the following
#if TABLE_SIZE>200
#undef TABLE_SIZE
#define TABLE_SIZE 200
The macro TABLE_SIZE is being compared
is it possible that its compared with a non numeric value like,
#if MACRO==ABCDEF123
I tried it but compiler complains of ABCDEF123 not being defined and assumes it as 0.
For true portability, the expression in #IF expression can only contain integer and character constants. The C and C++ preprocessors can also evaluate +, -, *, /, <<, >>, !=, == and the two logical operators && and || which obey the short-circuiting rules of standard C and C++.
So no, you can't compare a string directly.

My own operators '+' , '-' [duplicate]

This question already has answers here:
What are the basic rules and idioms for operator overloading?
(8 answers)
Closed 7 years ago.
Is it possible to create my own operators like '+' or '/'
Is it possible in C++?
I have already found operator '#' , but I do not know how to do this with another.
You can overload +, - for a given class, so they have custom behavior.
You can define preprocessor macros like #define OP(a,b) ((a))-(b)*(a)) and write code like 3 OP 4.
Other than that, I don't believe there's anything deep you can do in C++ to create your own new tokens or syntax. You can always write your own programming language - many people do - and creating a new operator pretty much means you're writing a new programming language by itself.
With the trivial Google search I found a table identifying 42 C++ operators that can be overloaded, and 4 C++ operators that cannot.
I did not verify this information.
If by create, you mean to define,
You can overload many defult operators for custon types, In fact for all of:
+ - * / % ˆ & | ~ ! = < > += -= *= /= %= ˆ= &= |= << >> >>= <<= == != <= >= && || ++ -- , ->* -> ( ) [ ]
Do do custom things for your classes.
You can also define functions, like float add(...) and define that to do whatever you like

or is not valid C++ : why does this code compile?

Here is a very simple C++ application I made with QtCreator :
int main(int argc, char *argv[])
{
int a = 1;
int b = 2;
if (a < 1 or b > 3)
{
return 1;
}
return 0;
}
To me, this is not valid C++, as the keyword or is not a reserved keyword.
But if I compile and run it, it works fine without any warnings ! The exit code is 0 and if I change b = 4, the exit code is 1 !
I'm not including anything to make sure there is no hidden define.
This is really strange to me. Is this something Qt is defining ? I didn't find anything in the documentation regarding that.
According to Wikipedia:
C++ defines keywords to act as aliases
for a number of symbols that function
as operators: and (&&), bitand (&),
and_eq (&=), or (||), bitor (|), or_eq
(|=), xor (^), xor_eq (^=), not (!),
not_eq (!=), compl (~).
As MadKeithV points out, these replacements came from C's iso646.h, and were included in ISO C++ as operator keywords. The Wikipedia article for iso646.h says that the reason for these keywords was indeed for international and other non-QWERTY keyboards that might not have had easy access to the symbols.
or is a C++ keyword, and you're allowed to use it instead of ||. There is no magic.
The same goes for and and most other logical operators. It's generally best to stick to the commonly known names though, to avoid confusion like this. If you use or, someone will wonder "why does this compile" ;)
iso646.h defines a number of operator alternatives - it's part of the C++ standard.