默认参数,gcc vs clang [英] default argument, gcc vs clang

查看:444
本文介绍了默认参数,gcc vs clang的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下:

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

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

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

VS 2013 and gcc 4.8.0 accept such code, while clang 3.3 reject such code with:


错误:在重新声明中添加默认参数会使此构造函数成为默认构造函数。

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

03和C ++ 11)的观点?

who is right from standard (C++03 and C++11) point of view?

注意:

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

I like clang's choice too, but I going to report bug to gcc and visual studio, and if this is not correct from standard point of view, this helps to convince compiler's developers to fix this issue.

GCC

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

I described issue here: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58194

但没有运气,他们暂停修正bug,直到草稿成为标准。

But no luck, they suspend bug fixing untill draft become standard.

推荐答案

这已在 Clang邮件列表,并且已作为缺陷报告核心问题1344。

This has been discussed on the Clang mailinglist and has been submitted as a Defect Report Core Issue 1344.

从邮件列表讨论:


这个想法是某些特殊成员的存在会影响类型类型的核心
属性,比如它是POD还是简单的
可复制。决定这些属性不应该需要全程序的
知识;对我们来说重要的是能够从
类定义中推导出它们。真正有问题的情况是通过添加默认
参数将
normal构造函数转换为复制或移动构造函数,但是IIRC引入一个默认构造函数也是
有问题。

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.

由WG21在布卢明顿会议上。来自
的注释:

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

共识:根据建议写出这样的错误核心
问题1344.优先级0, Doug起草。

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

因此,CWG已经同意(原则上)这应该是错误的。

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

TL; DR Clang是正确的,只要缺陷得到修复(不确定这是否可以正式只发生在C ++ 14,或者如果委员会的决定也可以做追溯到C ++ 11)

TL;DR Clang is right whenever the defect gets fixed (not sure if that can officially only happen with C++14, or if such Committee decisions can also be done retroactively on C++11)

这篇关于默认参数,gcc vs clang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆