4

My daughter's 12th standard C++ textbook says that

the notation of an array can (also) be given as follows: Array name [lower bound L, upper bound U]

This was a surprise for me. I know Pascal has this notation, but C++? Had never seen this earlier. I wrote a quick program in her prescribed compiler (the ancient Turbo C++ 4.5), and that does not support it. Did not find this syntax in Stanley Lippman's book either. Internet search did not throw up this. Or maybe I didn't search correctly?

So, is it a valid declaration?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Pradyumn
  • 400
  • 1
  • 2
  • 15
  • 2
    PL/1 lets you do that, also, but not C++. – stark Dec 04 '14 at 15:22
  • 4
    It's certainly not the case in any standard since at least 1998. Learning an obscure twenty-year-old dialect is a massive waste of time and effort; I'd throw away that textbook (and that compiler) and get one of [these](http://stackoverflow.com/questions) and a recent version of GCC or Clang. – Mike Seymour Dec 04 '14 at 15:29
  • 1
    Eww. Change her school if they are using Turbo C++. – crashmstr Dec 04 '14 at 15:29
  • Note the capital A in "Array name[...]". I suspect an Array class that is specified elsewhere in the book. Without the book title, we're only to guess here. Some classes do overload the comma operator, like Boost lambdas, but for an array class (especially for educational purposes) it would seem inadvisable. – Jonny D Dec 04 '14 at 21:47

2 Answers2

3

No it's not true, unless someone has overloaded the comma operator and possibly [] as well which is very unlikely. (Boost Spirit does both but for very different reasons).

Without any overloading at all, Array[x, y] is syntatically invalid since the size must be a constant-expression and these cannot contain the comma operator; as to do so would make it an assignment-expression.

Burn the book and put Stroustrup in her Christmas stocking!

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 2
    No, syntax like `Array[x,y]` is invalid since the size must be a *constant-expression*, which can't contain the comma operator (since that makes it an *assignment-expression*). – Mike Seymour Dec 04 '14 at 15:32
  • @MikeSeymour: Mike, that's a very good point. If you don't mind, I've incorporated it into the answer. – Bathsheba Dec 04 '14 at 15:37
3

This is not valid, from the draft C++ standard section 8.3.4 Arrays the declaration must be of this form:

D1 [ constant-expressionopt] attribute-specifier-seqopt

and we can from section 5.19 Constant expressions the grammar for constant expression is:

constant-expression:
   conditional-expression

This grammar does not allow us to get to the comma operator either to do something like this:

int a[ 1, 2 ] ;
        ^

as others have implied since there is no path to comma operator from conditional-expression. Although if you add parenthesis we can get to the comma operator since conditional-expression allows us to get to primary-expression which gets us () so the following would be valid:

int a[ (1, 2) ] ;
       ^   ^

Note, in C++03 you were explicitly forbidden from using the comma operator in a constant expression.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740