变量函数中...
之前的最后一个参数的类型可以是枚举类型吗?
enum
种类型是否会经历默认参数提升?
在某些情况下,不能安全地使用经过默认参数提升的类型,vararg处理(va_start
,va_arg
)就是一个明显的例子.我使用的编译器似乎将enum
类型视为int
,这在这些上下文中使用是安全的.这种情况一直都是这样吗?我可以安全地使用枚举类型作为变量函数中...
之前的最后一个参数吗?
变量函数中...
之前的最后一个参数的类型可以是枚举类型吗?
enum
种类型是否会经历默认参数提升?
在某些情况下,不能安全地使用经过默认参数提升的类型,vararg处理(va_start
,va_arg
)就是一个明显的例子.我使用的编译器似乎将enum
类型视为int
,这在这些上下文中使用是安全的.这种情况一直都是这样吗?我可以安全地使用枚举类型作为变量函数中...
之前的最后一个参数吗?
可以,根据为enum
Select 的基础类型,可以进行默认参数提升.
摘自第C standard节中关于枚举说明符的6.7.2.2节:
Each enumerated type shall be compatible with 100, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined, but shall be capable of representing the values of all the members of the enumeration.枚举类型为 不完整,直到紧跟在终止列表的
}
之后 枚举数声明,然后完成
因此,实现可以 Select 使用小于int
的类型作为基础类型,如果是这样的话,它将受到默认参数提升的影响.如果您指定-fshort-enums
旗,GCC尤其会这样做.
如果是这样的话,你就不能把这enum
作为变量函数的最后一个命名参数.摘自关于va_start
的第7.16.1.4p4节:
参数
parmN
是中最右边的参数的标识符 函数定义中的变量参数列表(刚刚 之前,...
).If the parameter 100 is declared with the 103 storage class, with a function or array type, or with a type that is not compatible with the type that results after application of the default argument promotions, the behavior is undefined.