C ++中有上下阵定义的制约? [英] C++ Array Definition with Lower and Upper Bound?
问题描述
我女儿的第12届标准C ++教科书说,
My daughter's 12th standard C++ textbook says that
数组的符号可以(也)给出如下:数组名
[下限L,上限C]
the notation of an array can (also) be given as follows: Array name [lower bound L, upper bound U]
这对我是一个惊喜。我知道帕斯卡尔有这个符号,但是C ++?从来没有见过这点。我在她的prescribed编译器(古代的Turbo C ++ 4.5)写了一个快速程序,并且不支持它。没有任何发现这个语法大师Stanley Lippman的书。互联网搜索没有抛出了这个。或者,也许我没有正确地搜索?
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?
推荐答案
这是无效的,从的草案的C ++标准部分 8.3.4
的阵列的声明必须是这种形式:
This is not valid, from the draft C++ standard section 8.3.4
Arrays the declaration must be of this form:
D1 [常量-EX pressionopt]属性说明符seqopt
D1 [ constant-expressionopt] attribute-specifier-seqopt
和我们可以从部分 5.19
的恒前pressions 的语法为恒前pression 是:
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 ] ;
^
正如其他人暗示,因为有没有路径的逗号操作符的距离的条件-EX pression 的。如果你加括号虽然我们可以获取到的逗号操作符的自的条件-EX pression 的让我们去的主要-EX pression 的这会让我们()
所以下面是有效的:
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) ] ;
^ ^
请注意,在C ++ 03你明确地在一个恒定的前pression使用逗号操作被禁止的。
Note, in C++03 you were explicitly forbidden from using the comma operator in a constant expression.
这篇关于C ++中有上下阵定义的制约?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!