gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:57:57 28 4
gpt4 key购买 nike

代码如下:

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/

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