ign*_*tec 20 c++ typedef const member-functions language-lawyer
假设我们想通过以下方式声明const成员函数typedef:
typedef int FC() const;
typedef int F();
struct A
{
FC fc; // fine, we have 'int fc() const'
const F fc; // not fine, 'const' is ignored, so we have 'int fc()'
};
Run Code Online (Sandbox Code Playgroud)
由于const被忽略,程序编译得很好.为什么const忽略功能?既然我们可以用这种方式形成const指针,我唯一能想到的就是"C传承".标准是否对此有所说明?
ale*_*in0 19
C++ 14标准,[dcl.fct] pt.7:
cv-qualifier-seq在函数声明符中的作用与在函数类型之上添加cv-qualification不同.在后一种情况下,忽略cv限定符.[注意:具有cv-qualifier-seq的函数类型不是cv限定类型; 没有cv限定的函数类型. - 结束说明]
例:
typedef void F();
struct S {
const F f; // OK: equivalent to: void f();
};
Run Code Online (Sandbox Code Playgroud)
所以,这是一种正确的行为.
| 归档时间: |
|
| 查看次数: |
779 次 |
| 最近记录: |