The word 'and' at c++ [duplicate] - c++

This question already has answers here:
When were the 'and' and 'or' alternative tokens introduced in C++?
(8 answers)
Closed 5 years ago.
Is the word and equivalent to the && operator?
if (inner > 10 and !id)
{
std::cout << "idle" << std::endl;
}
This code was originally translated from Python.
I was sure that the 'if' line would result in a compilation error. But it does pass.
Visual studio (2015) marks it as an error, but it does compile with g++ (and also on this site https://www.onlinegdb.com/online_c++_compiler) and seems to run as expected.
Is this correct syntax or did I miss something?

Yes, according to:
https://en.wikipedia.org/wiki/Digraphs_and_trigraphs#C++
and is equivalent to && ... another issue is that it is not really widely used and it is one character longer than && ... and we know that C++ programmers try to optimize everything. Even the length of their source code, so don't expect to find it widespread in production code.

Related

why this works: C++ last statement as result of expression [duplicate]

This question already has answers here:
Are compound statements (blocks) surrounded by parens expressions in ANSI C?
(2 answers)
Warning "Use of GNU statement expression extension"
(4 answers)
Closed 7 years ago.
I found strange macros in a driver implementation that I can't explain to myself.
Simplified example is:
cout << ({int i=0; while(i<10) {++i;} i;}) << endl;
It will output 10.
But why this does expression become an rvalue at all? It seems to work in C and in C++.
Can someone explain me? Pointing to keywords and to reference will be great.
The is a GCC extension:
A compound statement enclosed in parentheses may appear as an
expression in GNU C.
The last thing in the compound statement should be an expression
followed by a semicolon; the value of this subexpression serves as the
value of the entire construct.

Strange error: stray ‘\226’ in this program [duplicate]

This question already has answers here:
"Stray '\226' in program" compiler errors [duplicate]
(2 answers)
Closed 7 years ago.
theRunners[i] |= (1ULL << (((runnerList)atoll(token)) – 1ULL));
Why is the line giving the following strange error?
error: stray ‘\226’ in program
What's wrong?
In the text you have pasted: the – sign is actually the character 0x96, which in the Windows-1252 code page is a sort of hyphen.
You must use the ASCII minus sign instead; try deleting that piece of code and re-typing it.
Make sure you are using a plaintext editor - some word processors will automatically change punctuation to "funky" alternative versions.

Comparing bool with 1 in C++ [duplicate]

This question already has answers here:
Can I assume (bool)true == (int)1 for any C++ compiler?
(5 answers)
Closed 7 years ago.
Is it correct to compare bool to 1?
In a legacy code I find often:
if (xyz.isCounterActive() == 1)
where sCounterActive() returns bool.
Obviously, if ( xyz.isCounterActive() ) is sufficient, but If I change this, I don't know which side-effects it may cause. Software is big, buggy but the customer insists, that it is working.
Compiler is VS2008
In this case result of xyz.isCounterActive() will be implicitly converted to int. There're many rules of implicit conversion, which can be found here, for example.
Probably signature of isCounterActive changed since it was introduced, and the one, who changed it, forgot to modify all isCounterActive calls.

Counter at Compile TIme [duplicate]

This question already has answers here:
Does C++ support compile-time counters?
(11 answers)
Closed 8 years ago.
I know this is a weird question, but I'm trying to find a way to analyze the code written by the user and collect some useful information that may be included in both "if" & "else" part.
Suppose I have an if-else statement,
counter = 0;
if( true )
++counter;
else
++counter;
is it possible that I can "enforce ++counter" to work and get "counter = 2" at compiler time? Template? Macro? Any other solution? Thanks in advance!
First of all, `counter' will never be 2. It will always be 1.
I'd say about 90% of current C++ compilers (running in "Release" mode") will recognize the invariant, and generate object code as if you had written:
counter = 0;
++counter;
Most will also go as far as
counter = 1;
However, such optimizations are not required by the Standard, so there is no way to "Force" a compiler to do them.
NOTE: The specifications for Java & C# do require compilers for those languages to recognize the invariant to do elide the if())

Literal "or" in c++ program? [duplicate]

This question already has answers here:
When were the 'and' and 'or' alternative tokens introduced in C++?
(8 answers)
Closed 8 years ago.
I'm translating a C++ function I wrote some time ago into python when I noticed that my C++ code contains the following lines:
if(MIsScaledOut()) {
if(DataType()==UnknownDataType or DataType()==h)
Descriptor = Descriptor + DataTypeString() + "OverM";
There's an or in there! This was probably because I previously translated from python, and forgot to switch to ||.
This code compiles in various OSes, with various compilers, and I've never seen a problem with it. Is this standard, or have I just gotten lucky so far, and this is something I should worry about?
After remembering the right word to google, I now see that it is listed as a C++ keyword, along with various similar keywords like and that I'd never seen (noticed?) before in C++. The reason these exist is because there are encodings that don't have some of the required punctuation characters used by the traditional operator spellings: {, }, [, ], #, \, ^, |, ~.
As #mafso points out, the alternative "spelled out" versions can be used in C by including the <iso646.h> header, which defines them as macros.
The question of which this has been marked duplicate also points out the existence of digraphs and trigraphs, which can be used to substitute for the missing characters. (That question also says "everybody knows about" them. Obviously, I did not...)