指向未指定的尺寸和QUOT的阵列;(* P)[]"用C ++违法但在C法律 [英] Pointer to array of unspecified size "(*p)[]" illegal in C++ but legal in C
问题描述
我刚刚发现这是(在C,但合法的)在C ++中非法的:
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
的#define ARRAY_LENGTH(A)(的sizeof(A)/的sizeof(A [0]))INT累加(INT N,const int的(*数组)[])
{
INT I;
INT总和= 0;
对于(i = 0; I< N ++我){
总和+ =(*数组)[我]
}
返回总和;
}INT主要(无效)
{
诠释一个[] = {3,4,2,4,6,1,-40,23,35};
的printf(%d个\\ N,积累(ARRAY_LENGTH(一),放大器;一));
返回0;
}
它编译没有使用问题的gcc -std = C89 -pedantic
,但无法使用 G ++
进行编译。当我尝试使用来编译g ++的
我收到以下错误消息:
的main.cpp:5:37:错误:参数'阵'包括指向未知的数组边界INT []
INT累加(INT N,INT(*数组)[])
^
main.cpp中:在函数'廉政的main():
main.cpp中:18:50:错误:无法转换'INT(*)[9]'到'INT(*)[]'的说法'2'到'廉政累加(INT,INT(*)[])'
的printf(%d个\\ N,积累(ARRAY_LENGTH(一),放大器;一));
我在我的C code一直在使用这个很长一段时间,我不知道,在C是非法++。对我来说,这似乎是记录一个函数具有一个数组,其大小手之前是不知道的好方法。
我想知道这是为什么合法的C,但是无效的C ++。我也想知道这是什么,使得C ++的委员会决定把它拿走(和分断用C这种兼容性)。
那么,为什么是这样的法律C code,但非法C ++ code?
丹萨克斯于1995年写了一篇关于这个,含铅量高达C ++标准化过程中:
该委员会决定功能,如这个,那个接受
指针或引用未知的数组边界,复杂化
声明匹配和C ++中的重载决策规则。该
委员会同意,因为这样的功能很少实用性和
很少见,这将是最简单的只是禁止他们。因此,
现在C ++草案规定:
如果一个参数的类型包括一个类型的形式指针的
未知的数组边界T或引用未知的数组边界的
T,是病态的节目。
块引用>
块引用>I just found out that this is illegal in C++ (but legal in C):
#include <stdio.h> #include <stdlib.h> #define ARRAY_LENGTH(A) (sizeof(A) / sizeof(A[0])) int accumulate(int n, const int (*array)[]) { int i; int sum = 0; for (i = 0; i < n; ++i) { sum += (*array)[i]; } return sum; } int main(void) { int a[] = {3, 4, 2, 4, 6, 1, -40, 23, 35}; printf("%d\n", accumulate(ARRAY_LENGTH(a), &a)); return 0; }
It compiles without problems using
gcc -std=c89 -pedantic
but fails to compile usingg++
. When I try to compile it usingg++
I get these error messages:main.cpp:5:37: error: parameter 'array' includes pointer to array of unknown bound 'int []' int accumulate(int n, int (*array)[]) ^ main.cpp: In function 'int main()': main.cpp:18:50: error: cannot convert 'int (*)[9]' to 'int (*)[]' for argument '2' to 'int accumulate(int, int (*)[])' printf("%d\n", accumulate(ARRAY_LENGTH(a), &a));
I have been using this in my C code for a long time and I had no idea that it was illegal in C++. To me this seems like a useful way to document that a function takes an array whose size is not known before hand.
I want to know why this is legal C but invalid C++. I also wonder what it was that made the C++ committee decide to take it away (and breaking this compatibility with C).
So why is this legal C code but illegal C++ code?
解决方案Dan Saks wrote about this in 1995, during the lead up to C++ standardisation:
The committees decided that functions such as this, that accept a pointer or reference to an array with unknown bound, complicate declaration matching and overload resolution rules in C++. The committees agreed that, since such functions have little utility and are fairly uncommon, it would be simplest to just ban them. Hence, the C++ draft now states:
If the type of a parameter includes a type of the form pointer to array of unknown bound of T or reference to array of unknown bound of T, the program is ill-formed.
这篇关于指向未指定的尺寸和QUOT的阵列;(* P)[]&QUOT;用C ++违法但在C法律的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!