In C ++, can we use {} to cast C-Style?

While I was reading about data type conversion, I saw this example:

void intval() { for (char c; cin >> c; ) cout << "the value of '" << c << "' is " << int{c} << '\n'; } 

I know that we can use with:

  • int(c)
  • (int) c
  • static_cast<int>(c)

My questions:

Q1: Is int{c} another way to cast data types?

Q2: 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 are the differences between 1 and 2? And how is int{c} different if it's just another casting way?

Q3: Are there other ways to explicitly transform / cast?

+7
c ++ casting type-conversion explicit-conversion
source share
3 answers

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); // warning only, still compiles int{10000000000ll}; // hard error mandated by the standard } 

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); // error (unsigned int)10000000; // compiles } 

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.

+8
source share

int(c) is a C ++ version of the cast in the style of C (int)c . First he tries const_cast<int>(c) , then (if that failed) static_cast<int>(c) and then reinterpret_cast .

int{c} is a slightly different fish bowler. Strictly speaking, this is list initialization and has more strict rules. In particular, narrowing transformations are not allowed, i.e.

 int x; char s{x}; // error 

Therefore, it is recommended that you use this (rather than a cast) unless you know that narrowing transformations are acceptable.

For types other than built-in, in addition to the above casts, there is also dynamic_cast .

+6
source share

Q1: Yes. This is almost the same as the functional style ( int(c) ) style, and uniform initialization works because of C ++ 11. However, initializing the brackets has several warnings, for example, narrowing conversions (for example, long l = 5; char c{l}; ) will result in a warning.

Q2: 1 and 2 are equivalent, although there are situations when one works and not the other.

 // long long(c); // Breaks unless you make a typedef for 'long long' (long long)c; // Works fine template <class In, class Out> Out convert(const In& in) { // return (Out)in; // Only works if 'In' is a primitive type return Out(in); // Works regardless of the type of 'In' (assuming an appropriate constructor exists) } 

Q3: The only C ++-style casting example you mention is static_cast . There are other C ++ translations:

  • dynamic_cast
  • reinterpret_cast
  • const_cast
+3
source share

All Articles