强制单参数构造函数在C

强制单参数构造函数在C

本文介绍了强制单参数构造函数在C ++中是显式的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,在C ++中,单参数构造函数可以用作隐式转换运算符。这可以通过将构造函数标记为显式来抑制。

By default, in C++, a single-argument constructor can be used as an implicit conversion operator. This can be suppressed by marking the constructor as explicit.

我希望将explicit设置为默认值,以便编译器不能静默地使用这些构造函数进行转换。

I'd prefer to make "explicit" be the default, so that the compiler cannot silently use these constructors for conversion.

有没有办法在标准C ++中做到这一点?如果没有,是否有一个pragma(或类似的),将在Microsoft C ++中工作这样做?关于g ++(我们不使用它,但它可能是有用的信息)?

Is there a way to do this in standard C++? Failing that, is there a pragma (or similar) that'll work in Microsoft C++ to do this? What about g++ (we don't use it, but it might be useful information)?

推荐答案

不,你必须做这一切都是手工。这是一个痛苦,但你肯定应该习惯于使单个参数构造函数显式。我不能想象如果你找到一个解决方案,然后不得不将代码移植到另一个平台,你会有痛苦。你通常应该避开这样的编译器扩展,因为它会使代码更不便携。

Nope, you have to do it all by hand. It's a pain, but you certainly should get in the habit of making single argument constructors explicit. I can't imagine the pain you would have if you did find a solution and then had to port the code to another platform. You should usually shy away from compiler extensions like this because it will make the code less portable.

这篇关于强制单参数构造函数在C ++中是显式的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 08:11