C++ 函数声明中的最大参数数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4582012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maximum number of parameters in function declaration
提问by Nawaz
I know that minimum number of parameters in function definition is zero, but what is the maximum number of parameters in function definition? I am asking the question just for the sake of knowledge and out of curiosity, not that I am going to write a real function.
我知道函数定义中的最小参数数为零,但是函数定义中的最大参数数是多少?我问这个问题只是为了知识和好奇心,而不是我要写一个真正的函数。
回答by Nawaz
Yes, there are limits imposed by the implementation. Your answer is given in the bold text in the following excerpt from the C++ Standard.
是的,实施存在限制。您的答案在以下 C++ 标准摘录中以粗体文本形式给出。
1. C++ Language
1. C++语言
Annex B - Implementation quantities
附件 B - 实施数量
Because computers are finite, C + + implementations are inevitably limited in the size of the programs they can successfully process. Every implementation shall document those limitations where known.This documentation may cite fixed limits where they exist, say how to compute variable limits as a function of available resources, or say that fixed limits do not exist or are unknown.
The limits may constrain quantities that include those described below or others. The bracketed number following each quantity is recommended as the minimum for that quantity.However, these quantities are only guidelines and do not determine compliance.
— Nesting levels of compound statements, iteration control structures, and selection control structures [256].
— Nesting levels of conditional inclusion [256].
— Pointer, array, and function declarators (in any combination) modifying an arithmetic, structure, union, or incomplete type in a declaration [256].
— Nesting levels of parenthesized expressions within a full expression [256].
— Number of characters in an internal identifier or macro name [1 024].
— Number of characters in an external identifier [1 024].
— External identifiers in one translation unit [65 536].
— Identifiers with block scope declared in one block [1 024].
— Macro identifiers simultaneously defined in one translation unit [65 536].
— Parameters in one function definition [256].
— Arguments in one function call [256].
— Parameters in one macro definition [256].
— Arguments in one macro invocation [256].
— Characters in one logical source line [65 536].
— Characters in a character string literal or wide string literal (after concatenation) [65 536].
— Size of an object [262 144].
— Nesting levels for #include files [256].
— Case labels for a switch statement (excluding those for any nested switch statements) [16 384].
— Data members in a single class, structure, or union [16 384].
— Enumeration constants in a single enumeration [4 096].
— Levels of nested class, structure, or union definitions in a single struct-declaration-list [256].
— Functions registered by atexit()[32].
— Direct and indirect base classes [16 384].
— Direct base classes for a single class [1024].
— Members declared in a single class [4 096].
— Final overriding virtual functions in a class, accessible or not [16 384].
— Direct and indirect virtual bases of a class [1 024].
— Static members of a class [1 024].
— Friend declarations in a class [4 096].
— Access control declarations in a class [4 096].
— Member initializers in a constructor definition [6 144].
— Scope qualifications of one identifier [256].
— Nested external specifications [1 024].
— Template arguments in a template declaration [1 024].
— Recursively nested template instantiations [17].
— Handlers per try block [256].
— Throw specifications on a single function declaration [256].
由于计算机是有限的,C++ 实现不可避免地受限于它们可以成功处理的程序的大小。每个实施都应记录那些已知的限制。该文档可能会引用存在的固定限制,说明如何根据可用资源计算可变限制,或者说固定限制不存在或未知。
这些限制可能会限制数量,包括以下描述的数量或其他数量。建议将每个数量后面的括号数字作为该数量的最小值。然而,这些数量只是指导方针,并不能确定合规性。
— 复合语句、迭代控制结构和选择控制结构的嵌套级别 [256]。
— 条件包含的嵌套级别 [256]。
— 指针、数组和函数声明符(任意组合)修改声明中的算术、结构、联合或不完整类型 [256]。
— 在完整表达式中嵌套括号表达式的级别 [256]。
— 内部标识符或宏名称中的字符数 [1 024]。
— 外部标识符中的字符数 [1 024]。
— 一个翻译单元中的外部标识符 [65 536]。
— 在一个块中声明的具有块作用域的标识符 [1 024]。
— 在一个翻译单元中同时定义的宏标识符 [65 536]。
— 一个函数定义中的参数 [256]。
— 一个函数调用中的参数 [256]。
— 一个宏定义中的参数 [256]。
— 一次宏调用中的参数 [256]。
— 一个逻辑源代码行中的字符 [65 536]。
— 字符串文字或宽字符串文字中的字符(连接后)[65 536]。
— 对象的大小 [262 144]。
— #include 文件的嵌套级别 [256]。
— switch 语句的 case 标签(不包括任何嵌套 switch 语句的标签)[16 384]。
— 单个类、结构或联合中的数据成员 [16 384]。
— 单个枚举中的枚举常量 [4 096]。
— 单个结构声明列表中的嵌套类、结构或联合定义的级别 [256]。
— atexit()[32] 注册的函数。
— 直接和间接基类 [16 384]。
— 单个类的直接基类 [1024]。
— 在单个类中声明的成员 [4 096]。
— 最终覆盖类中的虚函数,无论是否可访问 [16 384]。
— 类的直接和间接虚拟基类 [1 024]。
— 类的静态成员 [1 024]。
— 类中的友元声明 [4 096]。
— 类中的访问控制声明 [4 096]。
— 构造函数定义中的成员初始值设定项 [6 144]。
— 一个标识符的范围限定 [256]。
— 嵌套外部规范 [1 024]。
— 模板声明中的模板参数 [1 024]。
— 递归嵌套模板实例化 [17]。
— 每个 try 块的处理程序 [256]。
— 在单个函数声明上抛出规范 [256]。
Besides, it also says in $18.3/6,
此外,它还说 $18.3/6,
Implementation Limits:The implementation shall support the registration of at least 32 functions.
实现限制:实现应支持至少 32 个函数的注册。
回答by Nawaz
2. C Language
2.C语言
5.2.4.1 Translation Limits
5.2.4.1 翻译限制
The implementation shall be able to translateand executeat least one programthat
contains at least one instance of every oneof the following limits:
— 127 nesting levels of blocks
— 63 nesting levels of conditional inclusion
— 12 pointer, array, and function declarators (in any combinations) modifying an
arithmetic, structure, union, or incomplete type in a declaration
— 63 nesting levels of parenthesized declarators within a full declarator
— 63 nesting levels of parenthesized expressions within a full expression
— 63 significant initial characters in an internal identifier or a macro name (each
universal character name or extended source character is considered a single
character)
— 31 significant initial characters in an external identifier (each universal character name
specifying a short identifier of 0000FFFF or less is considered 6 characters, each
universal character name specifying a short identifier of 00010000 or more is
considered 10 characters, and each extended source character is considered the same
number of characters as the corresponding universal character name, if any)
— 4095 external identifiers in one translation unit
— 511 identifiers with block scope declared in one block
— 4095 macro identifiers simultaneously defined in one preprocessing translation unit
— 127 parameters in one function definition
— 127 arguments in one function call
— 127 parameters in one macro definition
— 127 arguments in one macro invocation
— 4095 characters in a logical source line
— 4095 characters in a character string literal or wide string literal (after concatenation)
— 65535 bytes in an object (in a hosted environment only)
— 15 nesting levels for #included files
— 1023 case labels for a switch statement (excluding those for any nested switch
statements)
— 1023 members in a single structure or union
— 1023 enumeration constants in a single enumeration
— 63 lev els of nested structure or union definitions in a single struct-declaration-list
实现应能够翻译和执行至少一个程序,该程序至少包含以下每一项限制的一个实例:
— 127 个嵌套级别的块
— 63 个嵌套级别的条件包含
— 12 个指针、数组和函数声明符(在任何组合中)修改声明中的算术、结构、联合或不完整类型
—完整声明符中带括号的声明符的
63 级嵌套 — 完整表达式中带括号的表达式的 63 级嵌套
— 内部标识符或宏名称中的 63 个重要首字母字符(每个通用字符名称或扩展源字符被视为单个字符)
— 外部标识符中的 31 个重要首字母字符(每个通用字符名称
指定一个 0000FFFF 或更少的短标识符被认为是 6 个字符,每个指定短标识符为 00010000 或更多的通用字符名称被认为是 10 个字符,每个扩展源字符被认为是与相应通用字符名称相同的字符数(如果有)
——4095 个外部标识符合二为一翻译单元
——在一个块中声明了 511 个具有块作用域的标识符
——在一个预处理翻译单元中同时定义了 4095 个宏标识符
— 一个函数定义中的
127 个参数 — 一次函数调用中的
127 个参数 — 一个宏定义中的
127 个参数 — 一次宏调用中的 127 个参数
— 逻辑源代码行中的
4095 个字符 — 字符串文字或宽字符串文字中的 4095 个字符(在连接)
— 对象中的 65535 个字节(仅在托管环境中)
— #included 文件的 15 个嵌套级别
— switch 语句的 1023 个 case 标签(不包括任何嵌套 switch 语句的标签)
— 单个结构或联合中的 1023 个成员
—单个枚举中的 1023 个枚举常量
— 单个结构声明列表中的 63 级嵌套结构或联合定义
However, it says in notes that,
然而,它在注释中说,
13) Implementations should avoid imposing fixed translation limits whenever possible.
13) 实现应尽可能避免强加固定的翻译限制。
回答by wallyk
As previous answerers have adequately noted, the number of parameters depends on the C++ compiler. The compiler limit may well be due to limitations imposed by hardware environments.
正如前面的回答者充分指出的那样,参数的数量取决于 C++ 编译器。编译器限制很可能是由于硬件环境施加的限制。
For example the VAX CPU architectureuses an 8-bit field which holds the number of arguments, so compliant VAX code cannot pass more than 255 parameters.
例如,VAX CPU 架构使用一个 8 位字段来保存参数的数量,因此兼容的 VAX 代码不能传递超过 255 个参数。
Likewise many small processors like the Intel 8048 familyare so constrained for memory that most C and C++ compilers give grief if more about four parameters are used. While many mainstream developers may sneer at the 8048, its descendants remain one of the most widely deployed CPUs—mostly in embedded devices.
同样,像Intel 8048 系列这样的许多小型处理器对内存的限制如此之大,以至于如果使用更多大约四个参数,大多数 C 和 C++ 编译器都会感到沮丧。虽然许多主流开发人员可能会对 8048 嗤之以鼻,但它的后代仍然是部署最广泛的 CPU 之一——主要是在嵌入式设备中。
Many RISC architecture implementations specify a parameter passing method using registers, rather than the stack. A basic ARM CPU, for example, has only 16 registers. The maximum number of parameters could well depend on compiler command line options which affect how it generates code to pass parameters. Maybe more than eight are pushed onto a stack. Maybe not.
许多 RISC 架构实现使用寄存器而不是堆栈指定参数传递方法。例如,一个基本的 ARM CPU 只有 16 个寄存器。参数的最大数量很可能取决于编译器命令行选项,这些选项会影响它生成代码以传递参数的方式。可能超过 8 个被压入堆栈。也许不吧。
If those examples seem overly restrictive, consider that the Palm OS limiteda C function to a single parameter, and that had to be of type int
or void *
.
回答by Sri Raghavan
Of course, if you're writing a function in C with such a large number of arguments, you probablywant to rethink the design of your program.
当然,如果您正在用 C 编写具有如此大量参数的函数,您可能需要重新考虑程序的设计。