简体   繁体   English

将 bool 或 float 分配给 int32_t 是否安全?

[英]Is it safe to assign bool or float to int32_t?

I have a c++ project that have a struct with int32_t member data type and i want to assign bool and float variables to it, will that ruin the value of the variable?我有一个c++项目,它有一个int32_t成员数据类型的结构,我想为它分配boolfloat变量,这会破坏变量的值吗? If yes, what should i do other than changing the struct member data type?如果是,除了更改结构成员数据类型之外,我还应该做什么?

Whenever the compiler has to use a value of a type in a context where another type is expected, an implicit conversion is performed.每当编译器必须在需要另一种类型的上下文中使用一种类型的值时,就会执行隐式转换。

The rules for implicit conversions are numerous , but under Floating–integral conversions, there is the following paragraph:隐式转换的规则 很多,但在浮动-整数转换下,有以下段落:

A prvalue of floating-point type can be converted to a prvalue of any integer type.浮点类型的纯右值可以转换为任何整数类型的纯右值。 The fractional part is truncated, that is, the fractional part is discarded.小数部分被截断,即小数部分被丢弃。 If the value cannot fit into the destination type, the behavior is undefined (even when the destination type is unsigned, modulo arithmetic does not apply).如果该值不能适合目标类型,则行为未定义(即使目标类型是无符号的,模运算也不适用)。 If the destination type is bool, this is a boolean conversion (see below).如果目标类型是 bool,这是一个布尔转换(见下文)。

So, you can safely assign a floating point type (eg float or double ) to an integer.因此,您可以安全地将浮点类型(例如floatdouble )分配给整数。 Provided that the value can fit, the decimal part will be truncated.如果该值可以容纳,小数部分将被截断。 This implies a loss of data, and might be a source of bugs, or might be done on purpose in certain applications.这意味着数据丢失,并且可能是错误的来源,或者可能是在某些应用程序中故意这样做的。 Note that the floating point types have a larger range than int32_t , and if the value cannot be stored, it is undefined behaviour as per the standard.请注意,浮点类型的范围大于int32_t ,如果无法存储该值,则按照标准,这是未定义的行为。

Bools, on the other hand, can be safely assigned to integer types under all circumstances:另一方面,布尔值在任何情况下都可以安全地分配给整数类型:

If the source type is bool, the value false is converted to zero and the value true is converted to the value one of the destination type (note that if the destination type is int, this is an integer promotion, not an integer conversion).如果源类型为 bool,则值 false 转换为零,值 true 转换为目标类型的值 1(请注意,如果目标类型为 int,则这是整数提升,而不是整数转换)。

The value you assign will be converted to int32_t .您分配的值将转换为int32_t The bool value will not lose anything. bool值不会丢失任何东西。 The float value will be truncated. float值将被截断。 Only the integral part of it will be stored.只会存储它的组成部分。

If yes, what should i do other than changing the struct member data type?如果是,除了更改结构成员数据类型之外,我还应该做什么?

That depends on whether the type of the values you want to store are determined at runtime or compile-time.这取决于您要存储的值的类型是在运行时还是在编译时确定。 If it's determined at runtime, you can use an std::any instead of an int32_t :如果它是在运行时确定的,则可以使用std::any而不是int32_t

#include <any>

struct MyStruct {
    std::any val;
};

// ...

MyStruct s;
s.val = true; // val now contains a bool
s.val = 3.1415; // val now contains a double
s.val = 3.1415f; // val now contains a float
s.val = 42; // val now contains an int

If the type is determined at compile-time, you can make a struct template:如果类型是在编译时确定的,你可以制作一个结构体模板:

template <typename T>
struct MyStruct {
    T val;
};

// ...

MyStruct<bool> s1;
s1 = false;

MyStruct<float> s2;
s2 = 3.1415;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM