为什么构造函数默认不显式? [英] why constructors aren't explicit by default?

查看:133
本文介绍了为什么构造函数默认不显式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很容易忘记标记一个构造函数显式:添加/删除参数,使它们是可选的等单一的可靠的方式我知道是声明每个构造函数作为显式,然后删除这个关键字只有当隐式是必需的设计(感谢允许这不仅在单参数构造函数的标准)。但是这看起来很丑陋。

It's so easy to forget to mark a constructor "explicit": adding/removing args, making them optional etc. the single reliable way I know is to declare every constructor as explicit and then remove this keyword only if implicitness is required by design (thanks to the standard that allows this not only on single-argument constructors). But this would look ugly.

不打算隐式构造函数打开(错误)隐式转换的使用,例如。作为此处。这可能发生意外,或可以打破向后兼容性

not intended implicit constructors open usage of (mistaken) implicit conversion, e.g. as here. this can happen by accident, or can break backward compatibility

所以为什么显式不是构造函数的默认特性,如果这将导致更少的错误?

so why "explicit" is not default characteristic of a constructor if this would lead to fewer bugs?

ps是的,我读了Stroustrup的C ++的设计和进化,只是不记得他是否对显式有任何说明

p.s. yeah, I read Stroustrup's "The Design and Evolution of C++", just don't remember if he says anything about "explicit" there

推荐答案

添加显式时的向后兼容性。语言是不断变化的,并且不断变化的语言中的现有结构的意义是一种确保的方式,使您的用户生气。现在仍然是这种情况,向下兼容性是联合会议员心目中的高度。

Backward compatibility at the time when explicit was added. The language was evolving and changing meaning of existing constructs in an evolving language is a sure way to make your users angry. It is still the case now, backward compatibility is something high in the mind of the comittee.

这篇关于为什么构造函数默认不显式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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