2

There is some "type of commutativity" between specifiers, as in:

int const arr[5] = { /*initial (and only) values*/};
const int arr[5] = { /* -//-*/};

where regardless of the sequence of the type name int and const specifier in the definition, the interpretation is the same: an array of const ints. Similarly, when accessing an predefined array element:

int a = arr[4];
int a = 4[arr];

there is equivalence between both ways of access.

Are there any other frequently used* similar cases?

*highly used expressions that exhibit similar behaviour and could be possibly misunderstood

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Ziezi
  • 6,375
  • 3
  • 39
  • 49
  • and see [this Q&A](http://stackoverflow.com/q/162480/819272) for your first question – TemplateRex Sep 22 '15 at 09:32
  • I'm not sure it is valid in C++ anymore, but math can solve it. `a = arr[4] => a = *(arr + 4) => a = *(4 + arr) => a = 4[arr]`. – masoud Sep 22 '15 at 09:34
  • @simplicisveritatis see http://stackoverflow.com/q/12005959/819272. And note that the recommendation is to ask a new question, not to reopen this one. – TemplateRex Sep 22 '15 at 09:37
  • 1
    so ask the 3rd question as a new question – TemplateRex Sep 22 '15 at 09:40
  • 2
    There is no relation whatsoever between `int const` order and the array index oddity. Also "are there any other similar cases?" is too broad, you need to ask the question in some manner of context, like a specific technical problem. – Lundin Sep 22 '15 at 09:49
  • [Here](http://stackoverflow.com/questions/3247285/const-int-int-const) is the duplicate for the other question about const. – Lundin Sep 22 '15 at 09:52
  • @Lundin fair enough! After the edit, the question is more general and attempts to address similar cases between other entities. The two examples are just for context, that is why they have no link between them only what appears to be a commutative property in the order they are applied – Ziezi Sep 22 '15 at 09:57
  • 2
    @simplicisveritatis But the correct answer would involve analysing the whole C language for "strange patterns"... which is why the question is far too broad (and lacks practical use). – Lundin Sep 22 '15 at 10:01
  • @TemplateRex but in the description says ...**edit to explain** how it different or ask new... So, why am I wrong to edit it and cast it for reopen? – Ziezi Sep 22 '15 at 10:02
  • @Lundin following your last comment, the question is edited to involve only highly used expressions that exhibit similar behaviour – Ziezi Sep 22 '15 at 10:06
  • 1
    Declarations are not expressions. There is no such in as 'commutativity between specifiers'. Commutativity is a property of an operator. Period. It is also fallacious to refer to type names as specifiers. If you're going to use technical jargon, please use it correctly. You will find that when you do so, your question collapses into a black hole of nothingness. Most of it merely concerns order-independence among certain keywords, which is a property of the grammar. – user207421 Sep 22 '15 at 10:12
  • @EJP isn't the assignment operator (in the OP definitions) an expression? – Ziezi Sep 22 '15 at 10:20
  • @EJP I am referring to type names by type specifiers, which is the general category: https://msdn.microsoft.com/en-us/library/4kw9aawy.aspx?f=255&MSPPError=-2147217396 – Ziezi Sep 22 '15 at 10:21
  • @EJP Thanks about the remarks, the constructive critique and the valuable information: order-independence is property of the grammar. – Ziezi Sep 22 '15 at 10:23
  • @EJP If you're going to use technical jargon, please use it correctly. Formally, `int` is a _type specifier_ (6.7.2). To name it type specifier is therefore the utmost correct form. – Lundin Sep 22 '15 at 10:59

0 Answers0