Почему вызов sizeof оператор с двумя аргументами?

Как насчет:

icon: [ icon0, icon1, icon2 ][area],
13
задан David Citron 8 June 2009 в 18:21
поделиться

4 ответа

On further research, I discovered that this is behavior specific to the WindRiver Diab compiler. Please see the EDIT in the question for details.

9
ответ дан 2 December 2019 в 00:47
поделиться

Мне это кажется простым применением оператора запятой , который вычисляет свой первый аргумент, отбрасывает результат, а затем оценивает его второй аргумент.

В В данном случае он определяет, имеет ли литерал 2 размер 4. Часть « var » не имеет значения.

3
ответ дан 2 December 2019 в 00:47
поделиться

Похоже на отвлекающий маневр. Я предполагаю, что вы случайно используете оператор запятой, а sizeof применяется к последнему значению.

1
ответ дан 2 December 2019 в 00:47
поделиться

As mentioned, the comma operator is being applied and sizeof is returning the size of an integer literal. Offhand this looks like an error on the author's part, but there could be some sinister coding happening.

sizeof expressions are not evaluated so they can be used for a number of tricky things. One example is to provide a reference for an otherwise unreferenced variable without causing the compiler to generate any code. See this article on creating a better assert macro for an example. Alexandrescu has some other examples of sizeof trickery in Modern C++ Design, if memory serves. It's possible, but not likely, that one of these non-obvious usages is intended.

Whatever the usage, if it's not commented in this situation then it's clearly not worth the trade-off in readability and should be changed.

0
ответ дан 2 December 2019 в 00:47
поделиться
Другие вопросы по тегам:

Похожие вопросы: