简体   繁体   English

理论上可以在c ++函数中将多少个参数作为参数传递?

[英]How many arguments can theoretically be passed as parameters in c++ functions?

I was wondering if there was a limit on the number of parameters you can pass to a function. 我想知道可传递给函数的参数数量是否受到限制。

I'm just wondering because I have to maintain functions of 5+ arguments here at my jobs. 我只是想知道,因为在我的工作中我必须保持5个以上参数的功能。

And is there a critical threshold in nbArguments , talking about performance, or is it linear? nbArguments是否有一个临界阈值,在讨论性能,还是线性的?

It seems like you're veering into subjective territory, considering that C varargs are (usually) passed mechanically the same way as other arguments. 考虑到C变量变量(通常)以机械方式与其他参数传递一样,似乎您正在进入主观领域。

The first few arguments are placed in CPU registers, under most ABIs. 前几个参数放在大多数ABI下的CPU寄存器中。 How many depends on the number of architectural registers; 多少取决于架构寄存器的数量; it may vary from two to ten. 它可能从2到10不等。 In C++, empty classes (such as overload dispatch tags) are usually omitted entirely. 在C ++中,通常会完全省略空类(例如重载派发标签)。 Loading data into registers is usually "cheap as free." 将数据加载到寄存器中通常是“廉价的”。

After registers, arguments are copied onto the stack. 寄存器之后,参数被复制到堆栈中。 You could say this takes linear time, but such operations are not all created equal. 您可以说这花费了线性时间,但是这样的操作并非全部相同。 If you are going to be calling a series of functions on the same arguments, you might consider packaging them together as a struct and passing that by reference. 如果要在相同的参数上调用一系列函数,则可以考虑将它们打包为一个struct ,并通过引用传递。

To literally answer your question, the maximum number of arguments is an implementation-defined quantity, meaning that the ISO standard requires your compiler manual to document it. 为了从字面上回答您的问题,参数的最大数量是实现定义的数量,这意味着ISO标准要求您的编译器手册对其进行记录。 The C++ standard also recommends (Annex B) that no implementation balk at less than 256 arguments , which should be Enough For Anyone™. C ++标准还建议(附件B)不要在少于256个参数的情况下实现 ,这对于任何人都应该足够。 C requires (§5.2.4.1) support for at least 127 arguments , although that requirement is normatively qualified such as to weaken it to only a recommendation. C要求(第5.2.4.1节)支持至少127个参数 ,尽管该要求在标准上具有限定性,例如可以将其削弱为仅建议。

Neither the C nor C++ standard places an absolute requirement on the number of arguments/parameters you must be able to pass when calling a function, but the C standard suggests that an implementation should support at least 127 parameters/arguments (§5.2.4.1/1), and the C++ standard suggests that it should support at least 256 parameters/arguments (§B/2). C和C ++标准都没有绝对要求调用函数时必须传递的参数/参数数目,但是C标准建议实现至少应支持127个参数/参数(第5.2.4.1节/ 1),并且C ++标准建议它应至少支持256个参数/参数(§B/ 2)。

The precise wording from the C standard is: C标准的确切措辞是:

The implementation shall be able to translate and execute at least one program that contains at least one instance of every one of the following limits. 该实现应能够翻译和执行至少一个程序,该程序包含以下每个限制的至少一个实例。

So, one such function must be successfully translated, but there's no guarantee that if your code attempts to do so that compilation will succeed (but it probably will, in a modern implementation). 因此,必须成功转换一个这样的函数,但不能保证如果您的代码尝试这样做,编译将成功(但在现代实现中可能会成功)。

The C++ standard doesn't even go that far, only going so far as to say that: C ++标准甚至没有走那么远,只是走得这么远:

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. 但是,这些数量仅是指导原则,并不能确定合规性。

As far as what's advisable: it depends. 就建议而言:这要视情况而定。 A few functions (especially those using variadic parameters/variadic templates) accept an arbitrary number of arguments of (more or less) arbitrary types. 一些函数(尤其是使用可变参数/可变模板的函数)接受(或多或少)任意类型的任意数量的参数。 In this case, passing a relatively large number of parameters can make sense because each is more or less independent from the others (eg, printing a list of items). 在这种情况下,传递相对大量的参数可能很有意义,因为每个参数或多或少都彼此独立(例如,打印项目列表)。

When the parameters are more...interdependent, so you're not just passing a list or something on that order, I agree that the number should be considerably more limited. 当参数更...相互依赖时,您不仅要传递列表或该顺序的内容,我同意数量应该受到更大的限制。 In C, I've seen a few go as high as 10 or so without being terribly unwieldy, but that's definitely starting to push the limit even at best. 在C语言中,我已经看到了一些会高达10左右,不受可怕笨拙,但是这绝对是开始,即使在最好的推向极限。 In C++, it's generally enough easier (and more common) to aggregate related items into a struct or class that I can't quite imagine that many parameters unless it was in a C-compatibility layer or something on that order, where a more...structured approach might force even more work on the user. 在C ++中,将相关项聚合到一个structclass中通常很容易(并且更常见),除非我在C兼容层或该顺序中的某个级别上,否则我无法想象这么多的参数。 ..结构化方法可能会迫使用户进行更多工作。

In the end, it comes down to this: you're going to either have to pass a smaller number of items that are individually larger, or else break the function call up into multiple calls, passing a smaller number of parameters to each. 最后,要归结为:您要么必须传递较小数量的单独较大的项目,要么将函数调用分解为多个调用,从而将较小数量的参数传递给每个调用。

The latter can tend to lead toward a stateful interface, that basically forces a number of calls in a more or less fixed order. 后者可能趋向于带状态的接口,该接口基本上以或多或少固定的顺序强制多个调用。 You've reduced the complexity of a single call, but may easily have done little or nothing to reduce the overall complexity of the code. 您已经降低了单个调用的复杂性,但是可能很容易就几乎没有采取任何措施来降低代码的整体复杂性。

In the other direction, a large number of parameters may well mean that you've really defined the function to carry out a large number of related tasks instead of one clearly defined task. 从另一方面讲,大量参数很可能意味着您已经真正定义了执行大量相关任务的功能,而不是一个明确定义的任务。 In this case, finding more specific tasks for individual functions to carry out, and passing a smaller set of parameters needed by each may well reduce the overall complexity of the code. 在这种情况下,为各个功能找到更具体的任务来执行,并传递每个功能所需的较小参数集,可能会大大降低代码的整体复杂度。

It is not really dirty, sometimes you can't avoid using 4+ arguments while maintaining stability and efficiency. 它不是很脏,有时您无法避免在保持稳定性和效率的同时使用4个以上的参数。 If possible it should be minimized for sake of clarity (perhaps by use of structs), especially if you think that some function is becoming a god construct (function that runs most of the program, they should be avoided for sake of stability). 如果可能的话,为了清楚起见应将其最小化(也许通过使用结构),特别是如果您认为某些功能正在成为上帝的构造(运行大多数程序的功能,出于稳定性考虑,应避免使用它们)。 If this is the case, functions that take larger numbers of arguments are pretty good indicators of such constructs. 如果是这种情况,则采用大量参数的函数可以很好地说明此类构造。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM