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? If yes, what should i do other than changing the struct member data type?

- 702
- 1
- 8
- 24

- 7
- 2
-
3It depends what you mean by "safe", `float` will be truncated to `int`, but it depends on you if that is safe or not. – Slava Apr 18 '19 at 20:07
-
2You can definitely *convert* `bool` to `int32_t` and `float` to `int32_t` but the difference is that second conversion can lead to data loss which is program defect. – user7860670 Apr 18 '19 at 20:07
-
2If your float value is 1.234, what do you expect to happen when you assign it to an int, that can only store integral values? – Marius Bancila Apr 18 '19 at 20:08
2 Answers
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).
So, you can safely assign a floating point type (e.g. float
or double
) to an integer. 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.
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).

- 8,827
- 1
- 23
- 37
The value you assign will be converted to int32_t
. The bool
value will not lose anything. The float
value will be truncated. 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
:
#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;

- 50,738
- 9
- 71
- 96