c++ - 默认参数,gcc vs clang

标签 c++ visual-c++ gcc c++11 clang++

代码如下:

struct Foo {
    Foo(const char *);
};

Foo::Foo(const char *str = 0)
{
}

VS 2013 和 gcc 4.8.0 接受这样的代码, 而 clang 3.3 拒绝这样的代码:

error: addition of default argument on redeclaration makes this constructor a default constructor

从标准(C++03 和 C++11)的角度来看,谁是正确的?

注意:

我也喜欢clang的选择,但是我要向gcc和visual studio报告bug, 如果从标准的角度来看这是不正确的,这有助于 说服编译器的开发者解决这个问题。

GCC

我在这里描述了问题:http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58194

但运气不好,他们会暂停错误修复,直到草案成为标准。

最佳答案

这已在 Clang mailinglist 上讨论过并已作为 Defect Report 提交核心问题 1344。

来自邮件列表讨论:

The idea is that the presence of certain special members affects core properties of a class type, like whether it's POD or trivially copyable. Deciding these properties should not require whole-program knowledge; it's important for us to be able to deduce them just from the class definition. The really problematic case is turning a "normal" constructor into a copy or move constructor by adding default arguments, but IIRC introducing a default constructor was also problematic.

The fix is that you should put the default argument in the initial declaration of the constructor.

This was last discussed by WG21 at the Bloomington meeting. Notes from there:

"Consensus: Make this ill-formed as suggested in the write-up. Core issue 1344. Priority 0, Doug drafting."

So CWG has agreed (in principle) that this should be ill-formed.

TL;DR 只要缺陷得到修复,Clang 就是正确的(不确定这是否只能在 C++14 中正式发生,或者此类委员会决定是否也可以在 C++ 上追溯执行11)

关于c++ - 默认参数,gcc vs clang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18313509/

相关文章:

c - 如何在 gcc 中执行 C89 风格的变量声明?

linux - 阐明 “./configure” 选项 “--build” 、 “--host” 和 “--target”

c++ - 将 int 添加到 char 数组

python - 通过套接字在 python 中读取 opencv 图像

c++ - 递归删除一个链表,使用new分配内存

c++ - Libnodave - 使用 TCP 连接的 daveStart() 错误

c++ - move 构造函数问题

c++ - 唯一指针内存泄漏

c - 使用 dpkg-buildflags 构建一个简单的程序

c - 为什么使用 D 选项编译头文件会比使用 C 包装器编译头文件产生更大的文件?