未初始化的布尔 [英] uninitialized bool

查看:96
本文介绍了未初始化的布尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编译并运行以下内容时:


#include< iostream>


int main(){

bool f;

std :: cout<< f<< std :: endl;

f = not(f);

std :: cout<< f<< std :: endl;

}


我得到的输出:

8

9


这是根据规格吗?我是否需要初始化布尔值?


罗伯特

解决方案

rs ******* @ gmail.com 写道:

当我编译并运行以下内容时:

#include< iostream>


bool f;
std :: cout<< f<< std :: endl;
f = not(f);
std :: cout<< f<< std :: endl;
}
我得到了输出:
8
这是根据规格?



是的,这个程序有不确定的行为。


我是否需要初始化布尔值?


如果你想避免像上面那样的东西,那么你必须

初始化布尔值。使用未初始化的变量未定义

行为。


john




rswans ... @ gmail.com写道:

当我编译并运行以下内容时:

#include< iostream>

int main(){
bool f;
std :: cout<< f<< std :: endl;
f = not(f);
std :: cout<< f<< std :: endl;
}
我得到了输出:
8
这是根据规格吗?我是否需要初始化布尔值?

Robert




是的。内在类型不会调用默认构造函数。你可以这样做

这个:


bool f = bool();


但它更好指定默认值是什么。


干杯! --M




< rs ******* @ gmail.com>在消息中写道

news:11 ******************** @ z14g2000cwz.googlegrou ps.com ...

当我编译并运行以下内容时:

#include< iostream>

int main(){
bool f;
标准: :cout<< f<< std :: endl;
f = not(f);
std :: cout<< f<< std :: endl;
}
我得到了输出:
8
9


输出可能是那个,任何其他价值,

或者什么也没有。该程序可以做任何*,

,因为它的行为是未定义的。

这是根据规范吗?我是否需要初始化布尔值?




* * any *类型的任何*对象必须已初始化或

之前已分配有效值它的值可以被评估,

否则行为是未定义的。使用标准库

类型,初始化通过默认的

构造函数自动执行。所以用一个声明如


std :: string s;


它看起来好像's''没有被初始化,但它是

(在这种情况下为空字符串)。


除了具有默认构造函数的类型(以及具有
$ b的对象) $ b静态存储持续时间),你应该*总是*在你定义它们时提供一个

的初始值。


-Mike


When I compile and run the following:

#include <iostream>

int main() {
bool f;
std::cout << f << std::endl;
f = not(f);
std::cout << f << std::endl;
}

I get as output:
8
9

Is this according to spec? Am I required to initialize booleans?

Robert

解决方案

rs*******@gmail.com wrote:

When I compile and run the following:

#include <iostream>

int main() {
bool f;
std::cout << f << std::endl;
f = not(f);
std::cout << f << std::endl;
}

I get as output:
8
9

Is this according to spec?



Yes, this program has undefined behaviour.

Am I required to initialize booleans?

If you want to avoid something like the above then yes you must
initialize the boolean. Using a uninitialised variable is undefined
behaviour.

john



rswans...@gmail.com wrote:

When I compile and run the following:

#include <iostream>

int main() {
bool f;
std::cout << f << std::endl;
f = not(f);
std::cout << f << std::endl;
}

I get as output:
8
9

Is this according to spec? Am I required to initialize booleans?

Robert



Yes. Intrinsic types don''t invoke default constructors. You could do
this:

bool f = bool();

But it''s better to specify what the default value is.

Cheers! --M



<rs*******@gmail.com> wrote in message
news:11********************@z14g2000cwz.googlegrou ps.com...

When I compile and run the following:

#include <iostream>

int main() {
bool f;
std::cout << f << std::endl;
f = not(f);
std::cout << f << std::endl;
}

I get as output:
8
9
The output could have been that, any other values,
or nothing at all. The program could do *anything*,
because its behavior is undefined.

Is this according to spec? Am I required to initialize booleans?



*Any* object of *any* type must have been initialized or
assigned a valid value before its value can be evaluated,
otherwise the behavior is undefined. With standard library
types, initialization happens automatically via a default
constructor. So with a statement such as

std::string s;

it might look as though ''s'' is not being initialized, but it is
(in this case to an empty string).

Except for types with default constructors (and objects with
static storage duration), you should *always* provide an
initial value when you define them.

-Mike


这篇关于未初始化的布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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