当您声明“指针类型"时,const是什么?形式(函数)参数“像const数组"? [英] What is const when you declare a "pointer type" formal (function) parameter "like an const array"?
问题描述
当您声明指针类型"形式(函数)参数像const数组"时,const
是什么?
What is const
when you declare a "pointer type" formal (function) parameter "like an const array"?
即:如果T是类型标识符,则为:
I.e.: If T is a type identifier, then is:
void f(const T arr[]);
等同于:
void f(const T * const arr);
或
void f(const T * arr);
或
void f(T * const arr);
又如何解密C/C ++标准来找到这种等效物? :-)
And how can one to decrypt the C/C++ standards to find this equivalents? :-)
关于上述声明的含义,C和C ++之间以及标准的不同形式之间是否存在差异? (对于C-符合ANSI C89和更高版本的标准)
Are there differences between C and C++ and between the different incarnations of the standards regarding the meaning of the declarations above? (for C - the standards of ANSI C89 and later are relevant)
推荐答案
对于C ++(目前问题也被标记为C):
For C++ (at the moment the question is also tagged as C):
这里涉及三种不同的等价形式. [1] const T arr[]
形式参数类型衰减为T const* arr
,因此在形式参数类型的上下文中是等效的. [2] T const* arr
等效于const T* arr
,这是表示它的另一种语法. [3]在函数类型的上下文中,T const* arr
中的形式参数类型等效于T const* const arr
,因为出于确定函数类型的目的而舍弃了顶级const
. IE.以这种方式不同的两个函数声明表示相同的函数.
There are three different kinds of equivalence involved here. [1] The const T arr[]
formal argument type decays to T const* arr
, which therefore is equivalent in the context of formal argument type. [2] T const* arr
is equivalent to const T* arr
, which is another syntax expressing the same. [3] In the context of function type the formal argument type in T const* arr
is equivalent to T const* const arr
, because top level const
is discarded for the purpose of determining function type. I.e. two function declarations that differ in this way denote the same function.
这篇关于当您声明“指针类型"时,const是什么?形式(函数)参数“像const数组"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!