C ++总是使用显式构造函数 [英] C++ always use explicit constructor

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

问题描述

阅读以下博客后:

http:// xania。 org / 200711 / ambiguous-overloading

我开始问自己我不应该总是显式地定义我的构造函数吗?

I started asking myself "should I not always explicit define my constructors?"

因此,我开始阅读的内容超过了这篇文章:

So I started reading more than found out this article :

http://www.sjbrown.co.uk/2004/05/01/always-use-explicit/

这是另一个例子,也解释了他背后的想法。
当然,这是一个博主的想法。

Which shows another example, and also explains his thoughts behind it. But of course this is one blogger's thoughts.

我很高兴听到你们的一些人,你对这种方式的想法,你的经验

I would be happy to hear from some of you,what your thought on the manner, what is your experience with the subject and a few example for either way would be nice.

推荐答案

传统的智慧是构造函数参数(通过使用默认参数显式或有效)应标记为 explicit ,除非它们定义了转换( std :: string 可从 const char * 转换为后者的一个示例)。你已经弄清楚了你自己的原因,因为隐式转换确实可以使生活比它更难。

The traditional wisdom is that constructors taking one parameter (explicitly or effectively through the use of default parameters) should be marked explicit, unless they do define a conversion (std::string being convertible from const char* being one example of the latter). You've figured out the reasons yourself, in that implicit conversions can indeed make life harder than it has to be.

一个明显的例外是复制构造函数。或者另一种方式是考虑大多数类型可以从和自己转换,因此复制构造函数在大多数时间没有标记为 explicit

A perhaps obvious exception to that would be the copy constructor. Or perhaps another way is to consider that most types are convertible from and to themselves, and that as such the copy constructor is not marked explicit most of the time.

虽然看起来标记所有其他类型的构造函数 explicit 没有损害,但我反对。因为虽然 explicit 对在C ++ 03中接受多个参数的构造函数没有影响,但它在C ++ 11中有效果。代码如下:

While it may appear that marking all other kinds of constructors explicit does not hurt, I'd argue against it. Because while explicit has no effect on a constructor taking multiple arguments in C++03, it does have an effect in C++11. To put it into code:

struct foo {
    explicit foo(int i);
    foo(int i, int j);
    explicit foo(int i, int j, int k);
};

foo make_foo()
{
    /* Not C++11-specific: */
    // Error: no conversion from int to foo
    return 42;

    // Okay: construction, not conversion
    return foo(42);

    // Okay: constructions
    return foo(42, 42);
    return foo(42, 42, 42);

    /* C++11 specific: */
    // Error: no conversion from int to foo
    return { 42 };

    // Not an error, not a conversion
    return { 42, 42 };

    // Error! Constructor is explicit
    return { 42, 42, 42 };
    // Not an error, direct-initialization syntax
    return foo { 42, 42, 42 };
}

我个人觉得在一个函数中返回 foo 我必须显式返回 foo {42,42,42} 。我没有看到显式正在保护我。我真的希望 {initializers ...} 语法意味着从给定的初始化器构造对象,并且显式获取进入的方式,同时拯救我从无。 (由于 {i} 在复制初始化的上下文中归结为 i -

I personally find it needlessly verbose that in a function that returns foo I have to explicitly return foo { 42, 42, 42 }. I don't see what explicit is protecting me from. I really want the { initializers... } syntax to mean 'construct object from given initializers', and explicit gets into the way of that while saving me from nothing. (Since { i } does boil down to i in the context of copy-initialization -- most of the time -- I'll gladly give that one up.)

所以我想说明使用 explicit 用于一元构造函数,和那些

So I'd say get into the habit of using explicit for unary constructors, and those only.

这篇关于C ++总是使用显式构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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