是否允许函数参数常量不匹配?

Is function parameter constness mismatch allowed?

关于 using const function parameters 我听说在某些 OS X 系统上,参数的常量性被破坏到函数签名中。例如,如果在接口头文件中有以下声明:

int f(int argument);

但如果只实现这个功能:

int f(int const argument);

那么这可能会导致 OS X 上的链接失败(但 Linux 上不会),因为 OS X 破坏 C++ 函数签名的方法包括参数的常量性.

哪个是正确的处理行为? C++ 标准对此有发言权吗?

出于名称重整的目的,应忽略顶级 const 和 volatile 限定符。这可以从标准中的两件事来确定。首先,函数的签名用于名称修改。

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

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节:

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 ]