gpt4 book ai didi

c++ - 默认参数提升何时发生?

转载 作者:行者123 更新时间:2023-11-30 04:17:01 27 4
gpt4 key购买 nike

在 C 语言中,当调用的函数没有原型(prototype)时,编译器会执行默认参数提升

但是 C++ 呢?默认参数提升何时发生?

在 C++11 标准 5.2.2/7 中:

When there is no parameter for a given argument, the argument is passed in such a way that the receiving function can obtain the value of the argument by invoking va_arg (18.10). [ Note: This paragraph does not apply to arguments passed to a function parameter pack. Function parameter packs are expanded during template instantiation (14.5.3), thus each such argument has a corresponding parameter when a function template specialization is actually called. —end note ] The lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions are performed on the argument expression. An argument that has (possibly cv-qualified) type std::nullptr_t is converted to type void* (4.10). After these conversions, if the argument does not have arithmetic, enumeration, pointer, pointer to member, or class type, the program is ill-formed. Passing a potentially-evaluated argument of class type (Clause 9) having a nontrivial copy constructor, a non-trivial move constructor, or a non-trivial destructor, with no corresponding parameter, is conditionally-supported with implementation-defined semantics. If the argument has integral or enumeration type that is subject to the integral promotions (4.5), or a floating point type that is subject to the floating point promotion (4.6), the value of the argument is converted to the promoted type before the call. These promotions are referred to as the default argument promotions.

本段仍未指定默认参数提升何时发生。这一段可能说的太多了,逻辑不清晰。我努力概述逻辑但失败了。我不熟悉调用 va_arg

希望你能帮助我。

最佳答案

默认提升将在调用上下文中调用函数之前发生。

如果您真的要问的是在什么情况下执行默认提升,摘录中已经涵盖了这一点,尽管它很小,很容易被忽略:“当给定参数没有参数时.. ”。换句话说,它本质上与 C 中的情况相同,只是 C++ 中根本不存在不指定参数类型的 C 风格函数声明。因此,只有当函数具有明确的省略号时,您才能拥有一个参数而不指定其类型的参数,例如 printf: int printf(char const *format, ... );

关于c++ - 默认参数提升何时发生?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17357898/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com