gpt4 book ai didi

c++ - va_arg 是否使用 pre- 或 post-mangled 类型作为它的第二个参数?

转载 作者:搜寻专家 更新时间:2023-10-31 01:07:03 25 4
gpt4 key购买 nike

当使用 C 级变量参数时:

void  example( size_t length, ... )
{
va_list list;
T x;
va_start( list, length );
//...
x = va_arg( list, T );
//...
va_end( list );
}

某些类型在通过“...”时会发生变化。例如,float 变成了 double。如果 T 是这样的类型,我是应该将原始类型还是损坏的类型放入 va_arg

如果 C 和 C++ 和/或标准修订版的答案不同,请指出。

(快速浏览这里表明应该使用损坏的类型。我希望不会,因为它被破坏了,因为它需要用户计算类型,如果原始类型在类型别名后面,这会很痛苦,尤其是没有通用原件的别名,例如 size_t。希望我的帖子标题更易于搜索。)

(经过深思熟虑,我意识到目标变量必须使用经过修饰的类型进行声明。因此,在 va_arg 中使用经过修饰的类型(如果为真)并没有我想象的那么愚蠢。我明白了有时会被 C++11 的 auto 宠坏。)

最佳答案

If T is such a type, am I supposed to put the original type or the mangled one into va_arg?

如果T是一个需要提升的类型,那么你应该放入转换后的类型,否则程序有未定义的行为。

If the answer differs for C and C++ and/or Standard revisions, please indicate that.

据我所知,C 和 C++ 都需要这个。


With more thought, I realize that the destination variable has to be declared with the mangled type

不,这不是必须的。因为可以保证对 va_arg() 的“调用”宏将是您指定为其第二个参数的类型,然后是类似

float f = va_arg(args, double);

会很好,因为va_arg(...)求值为 double 类型的表达式, 然后将隐式转换为 float .


编辑:一些标准引语。

我。 C99,7.15.1.1.2:

[...] If there is no actual next argument, or if type is not compatible with the type of the actual next argument (as promoted according to the default argument promotions), the behavior is undefined [...].

二。 C++11 工作草案,18.10.3:

The restrictions that ISO C places on the second parameter to the va_start() macro in header <stdarg.h> are different in this International Standard. The parameter parmN is the identifier of the rightmost parameter in the variable parameter list of the function definition (the one just before the ...). If the parameter parmN is declared with a function, array, or reference type, or with a type that is not compatible with the type that results when passing an argument for which there is no parameter, the behavior is undefined.

对我来说,上面的 (C++) 引述似乎与默认促销行为无关;但是,我认为说“限制不同”意味着它们是相同的,除了以下 [above] 条款。

关于c++ - va_arg 是否使用 pre- 或 post-mangled 类型作为它的第二个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19876414/

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