隐式构造函数与“空”构造函数的比较建设者 [英] Implicit constructor versus "empty" constructor

查看:87
本文介绍了隐式构造函数与“空”构造函数的比较建设者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中,除了构造函数外,模板结构BB和CC几乎相同。模板BB使用不执行任何操作的构造函数,而模板CC使用默认的构造函数。当我使用Visual Studio 2013更新4对其进行编译时,在声明 constInst2 的行中引发错误,但在声明 constInst 的行中未引发错误。 code>:


错误C4700:使用未初始化的局部变量'instance2'




在初始化实例时,我也期望出现相同的错误。我是否误解了这句话


如果隐式声明的默认构造函数未删除或
是微不足道的,它由编译器定义(即,生成函数主体并编译
),其效果与具有空主体和空初始化器的
用户定义构造函数完全相同




  struct AA 
{
typedef int a;
typedef const int b;
};

template< typename A>
结构BB
{
类型名A :: a a_A;
typedef typename A :: b a_B;

BB()
{};
};

模板<类型名称A>
struct CC
{
typename A :: a a_A;
typedef typename A :: b a_B;

CC()=默认值;
};

int main()
{
BB< AA>实例
BB< AA> :: a_B constInst(instance.a_A);

CC< AA> instance2;
CC< AA> :: a_B constInst2(instance2.a_A);

返回0;
}


解决方案

其中有一个编译器标志Visual Studio将警告视为错误(/ WX)。您可以关闭该标志,以将警告视为错误。您还可以选择忽略特定的警告(/ wd4100以禁用警告C4100)。



您看到的是被视为错误的编译器警告。

p>

这与标准报价的解释无关。



如果

  BB< AA>实例

编译器不会发出警告消息,因为您可能在构造函数中做一些有副作用的事情。



对于

>

  CC< AA> instance2; 

它能够推断出构造对象没有副作用。


In the following code, template structures BB and CC are almost identical except for the constructors. Template BB uses a constructor that does nothing whereas template CC uses the default constructor. When I compile it using Visual Studio 2013 update 4, an error is thrown in the line that declares constInst2 but not on the line that declares constInst:

error C4700: uninitialized local variable 'instance2' used"

I expected the same error when initializing 'instance' as well. Am I misinterpreting this sentence?

"If the implicitly-declared default constructor is not deleted or trivial, it is defined (that is, a function body is generated and compiled) by the compiler, and it has exactly the same effect as a user-defined constructor with empty body and empty initializer list."

struct AA
{
    typedef int a;
    typedef const int b;
};

template< typename A >
struct BB
{
    typename A::a a_A;
    typedef typename A::b a_B;

    BB()
    {};
};

template< typename A >
struct CC
{
    typename A::a a_A;
    typedef typename A::b a_B;

    CC() = default;
};

int main()
{
    BB< AA > instance;
    BB< AA >::a_B constInst( instance.a_A );

    CC< AA > instance2;
    CC< AA >::a_B constInst2( instance2.a_A );

    return 0;
}

解决方案

There is a compiler flag in Visual Studio to treat warnings as errors (/WX). You can turn that flag off to not treat warnings as errors. You can also choose to ignore specific warnings (/wd4100 to disable warning C4100).

What you are seeing is a compiler warning that is being treated as an error.

This is unrelated to the interpretation of the quote from the standard.

In case of

BB< AA > instance;

the compiler does not issue a warning message since you could be doing something in the constructor that has side effects. The compiler is choosing not to delve into the details of how the constructor is implemented to deduce whether calling the constructor has side effects or not.

In the case of

CC< AA > instance2;

it is able to deduce that there are no side effects of constructing the object.

这篇关于隐式构造函数与“空”构造函数的比较建设者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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