Type safety is a big thing in C++. Variables of different types which don't support implicit conversions can not be set to be equal. How this safety check is performed? In addition to the variable itself, is there some information about type stored for that variable?
The variable itself doesn't contain any information about what it contains. Instead C++ uses its compile step to perform a wide array of verification steps to ensure that everything will work correctly during run time.
Put simply if I have the following function:
double convert(int32_t num)
{
return static_cast<double>(num);
}
It compiles into a procedure which takes its one and only parameter and performs a 32-bit integer to 64-bit floating point conversion. If you gave it a 32-bit floating point number it would do the incorrect thing.
However the type system ensures that anyone who calls convert
supplies a 32-bit integer (note the compiler may insert a conversion on your behalf in some cases, but the function only ever sees a 32-bit integer) and thus there is no need for a run-time check that what you supplied is actually a 32-bit integer.
The type system does exist at compile time of course and is tracked quite carefully. Take the following example:
int32_t x = 12;
double y = convert(x);
The compilers view of this includes that x
is an int32_t
, convert
takes a int32_t
and returns a double
and y
is a double
. Since those all line up no conversion is necessary and it compiles successfully. In contrast:
char* x = "12";
double y = convert(x);
Doesn't compile because char *
is not the same (and is not convertible) to int32_t
.