是函数参数constness不匹配吗? [英] Is function parameter constness mismatch allowed?

查看:183
本文介绍了是函数参数constness不匹配吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于使用 const 函数参数我听说在某些OS X系统中,参数的常数被转换为函数签名。例如,如果在接口头文件中有如下声明:

Regarding using const function parameters I've heard that on some OS X systems the constness of a parameter is mangled into the function signature. For example, if one would have the following declaration in an interface header file:

int f(int argument);

但如果只实现此功能:

int f(int const argument);

那么这可能导致OS X上的链接失败方式对C ++函数签名包括参数的常量。

then this might lead to a linking failures on OS X (but not on Linux) because the OS X way to mangle C++ function signatures includes the constness of the parameters.

这是正确的mangling行为? C ++标准在这方面有什么意见吗?

Which is the correct mangling behaviour? Does the C++ standard have a say on this?

推荐答案

顶级const和volatile限定符应该被忽略名称mangling。这可以从标准中的两件事情确定。

The top-level const and volatile qualifiers should be ignored for the purposes of name mangling. This can be determined from two things in the standard. The first, is that a function's signature is used for name mangling.

在C ++ 14标准中,1.3.17节定义了一个签名:

In the C++14 standard, section 1.3.17 defines a signature:


名称,参数类型列表(8.3.5)以及包含命名空间(如果有)[注意: - 结束注释]

name, parameter type list (8.3.5), and enclosing namespace (if any) [ Note: Signatures are used as a basis for name mangling and linking. — end note ]

要获取参数类型列表的定义,我们然后参考第8.3.5 / 5节:

To get the definition of a parameter type list, we then refer to section 8.3.5/5:


函数的类型使用以下规则确定。每个参数
(包括函数参数包)的类型由它自己的decl-specifier-seq和declarator确定。在
确定每个参数的类型之后,类型数组T或返回T的函数的任何参数分别是被调整为指向T的指针或返回T的函数的指针的
。在生成参数类型的列表
之后,在形成
函数类型时,会删除修改参数类型的任何顶级cv-qualifiers。转换的参数类型的结果列表以及省略号
或函数参数包的存在或缺失是函数的参数类型列表。 [注意:这个转换不会
影响参数的类型。例如,int()(const int p,decltype(p))和int()(int,
const int
)是相同类型。 - end note]

The type of a function is determined using the following rules. The type of each parameter (including function parameter packs) is determined from its own decl-specifier-seq and declarator. After determining the type of each parameter, any parameter of type "array of T" or "function returning T" is adjusted to be "pointer to T" or "pointer to function returning T," respectively. After producing the list of parameter types, any top-level cv-qualifiers modifying a parameter type are deleted when forming the function type. The resulting list of transformed parameter types and the presence or absence of the ellipsis or a function parameter pack is the function’s parameter-type-list. [ Note: This transformation does not affect the types of the parameters. For example, int()(const int p, decltype(p)) and int()(int, const int) are identical types. — end note ]

这篇关于是函数参数constness不匹配吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆