Is int{c} another way to cast data types?
Yes. T{value} creates a temporary type T , which is initialized with a direct list with the specified braced-init list. This listing has the advantage over T(value) in that T{value} can be used to create a temporary array. This will be done, for example,
int main() { using int_array = int[5]; for( auto e : int_array{1,2,3,4,5}) std::cout << e; }
It also contains a warning that narrowing a conversion is a mistake.
int main() { int(10000000000ll);
After some research on the net, I know that C ++ casting is different, and it has a compiler that checks whether casting is possible at compile time, but what is the difference between 1 and 2?
The big difference between T(value) and (T)value is that in T(value) , T should be one word. for example
int main() { unsigned int(10000000);
Q3: Are there other ways to explicitly transform / cast?
Well in C ++ they want you to use C ++ casts that are static_cast , reinterpret_cast , dynamic_cast and const_cast . They are preferable to the c style, chosen as the c-style, will do all those where C ++ versions have certain restrictions and come with certain guarantees.
Nathan oliver
source share