gpt4 book ai didi

c++ - ctor 声明/定义中接受的 const 限定符(llvm 错误?)

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:03:00 25 4
gpt4 key购买 nike

我的编译器(实际上是 Apple LLVM 5.0 版(clang-500.2.79)(基于 LLVM 3.3svn))接受(编译)该代码:

class X {
private:
int i;
public:
const X() { cout << "here" << endl; i=0; }
void f() const {}
void g() {}
};

int main() {
const X x;
x.f();
// x.g();
X y;
y.f();
y.g();
}

它的工作方式就好像没有 const 限定符引导 ctor 定义一样。我尝试了 -Wall-pedantic 不同类型的标准激活,总是一样的......所以:

  • 我错过了什么吗?我没能发现它在最新标准中的句法是正确的……
  • 这是 gcc/llvm 的错误吗?似乎 gcc/llvm 默默地忽略了 const
  • 这是我错过的功能吗?我的示例无法证明它的用处吗?

注意:gcc 3.4.3 不编译它,gcc 4.4.5 也不编译。

最佳答案

据我所知,这看起来不像是有效语法,draft C++ standard 12.1 Constructors1 说:

Constructors do not have names. A special declarator syntax is used to declare or define the constructor. The syntax uses:

— an optional decl-specifier-seq in which each decl-specifier is either a function-specifier or constexpr,

— the constructor’s class name, and

— a parameter list

7.1.2部分我们可以看到函数说明符如下:

function-specifier:
inline
virtual
explicit

通过阿里在comment above一个bug report已为此提交,已确认并已解决。

关于c++ - ctor 声明/定义中接受的 const 限定符(llvm 错误?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19935868/

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