What is this Called in C++? [duplicate] - c++

This question already has answers here:
What is a designated initializer in C?
(2 answers)
Closed last month.
I've seen a piece of code that goes like this:
int charP[] = {
['A'] = 1,
['B'] = 2,
['C'] = 3,
['D'] = 4};
My syntax highlighter for C++ does not recognize it, but the syntax highlighter for C does. It sort of behaves like a Python dictionary, so charP['A'] would spit out 1. Maybe I'm not looking hard enough, but can anyone point me in the right direction because every time I search "C++ dictionary" or "C dictionary", it always involves Maps and not this. What is it? What are its limitations?

In C it's array initialization using a designated initializer. It's covered in section 6.7.9 of the C 2017 specification.
In C++ it's invalid syntax (#Ranoiaetep, #CaptainObvlious).

Related

Testing if an expression compiles [duplicate]

This question already has answers here:
Portable way to check if expression compiles
(2 answers)
How to check at compile time that an expression is illegal?
(2 answers)
Closed 3 days ago.
Following this question where I ask how to test if an expression that tries to use an input iterator for write compiles (disclaimer, it shouldn't!), I would like to know if it's possible to have a generic solution that, given an expression, we are able to check if the expression will compile or not.
Take an easy example, the input iterator in the linked post. If I try to write with an input iterator:
collections::Array arr = collections::Array<int, 5>{1, 2, 3, 4, 5};
auto it_begin = arr.begin();
*it_begin = 7;
that shouldn't compile, because the operator*() returns a const&.
So, what would be the way, of, given an expression, test if it will compiles?
Extra points (well, just take this in consideration if it's possible):
No macros involved
No SFINAE related stuff

Is there a C or C++ equivalent to 'pass' in python? [duplicate]

This question already has answers here:
Is there an equivalent of Python's `pass` in c++ std11?
(7 answers)
Closed 2 years ago.
Is there a C or C++ equivalent to 'pass' in python? Also the same with break. For example:
while True:
if x == 1:
break
else:
pass
But in C/C++ instead?
Is there a C or C++ equivalent to 'pass' in python?
Yes. There are actually two equivalents. One is the null statement:
;
Another is the empty block statement:
{}
These are in most cases inter-changeable except the null statement cannot be used in all cases where the empty block statement can. For example, the function body must be a block statement.
In the example case, you can omit the else-statement entirely just like you can in Python.

Does anybody see such an array indexing way like "2[array]"? [duplicate]

This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 8 years ago.
Today, I see such a wierd way of indexing the arry.
The code is like:
int array[] = {10, 20, 30};
cout << -2[array];
I've never seen such a strange way of using array. But there is no compilation error.
Can anyone tell me does the ISO document evolve the description for this way of using array?
It works, because expressions of the form x[y] is just sugar for *(x+y), and of course, the addition is commutative, so 2[array] and array[2] get compiled to the same thing.
Don't do it though, because it's unnecessarily confusing.

and-or trick in Python [duplicate]

This question already has answers here:
Is short-circuiting in assignment statements considered good style?
(2 answers)
Closed 9 years ago.
What's the purpose of the "and-or" trick?
i.e.
>>> a = ""
>>> b = "second"
>>> 1 and a or b
'second'
It was just a way to mimic the conditional operator (aka. "ternary" operator) found in the C family of languages. In the past, there was no direct equivalent expression. The following expressions are somewhat equivalent:
# in python
a and b or c
// in C
a ? b : c
Don't use it though. Due to the semantics of Python, if b was falsy, the expression will evaluate to c.
They have since provided a proper syntax for this construct as of Python 2.5 (PEP 308).
b if a else c

what does <?= meaning in C++ [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
C++ weird usage of conditional operator (>?=)
C extension: <? and >? operators
When i read a C++ code,i see the following lines:
void add(double v) { min <?= v; max >?= v; StatFig::add(v); }
What does the
>?=
means?
Thanks.
Answered right here: Link.
As explained by the great answer there which i in no way take credit for, it's a deprecated GCC extension, and in no way standard C++. Avoid.
There is a list of operators in C++ on wikipedia, but it's not in here, so maybe it's defined elsewhere in the code you read.
My guess would be that's it's an assignement operator that checks if the value is lower/higher than a certain threshold, and if so assigns that threshold to the variable. Or something
In other words, min <?= v might be equivalent to min=((min<v)?min:v), but that's just a guess.