Best practice in C++ for casting between number types

This question already has an answer here:

  • When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used? 6 answers

  • Just use static_cast . The problem with C casts is the ambiguity of the operation (ie point (1) of Explicit type conversion).

    C++ casts avoid this. Additionally C++ casts are more visible when searching for them.

    Using Stroustrup's words (What good is static_cast?):

    Even an innocent-looking cast can become a serious problem if, during development or maintenance, one of the types involved is changed. For example, what does this mean?:

      x = (T)y;
    

    We don't know. It depends on the type T and the types of x and y. T could be the name of a class, a typedef , or maybe a template parameter. Maybe x and y are scalar variables and (T) represents a value conversion. Maybe x is of a class derived from y 's class and (T) is a downcast. Maybe x and y are unrelated pointer types. Because the C-style cast (T) can be used to express many logically different operations, the compiler has only the barest chance to catch misuses. For the same reason, a programmer may not know exactly what a cast does. This is sometimes considered an advantage by novice programmers and is a source of subtle errors when the novice guessed wrong.

    The "new-style casts" were introduced to give programmers a chance to state their intentions more clearly and for the compiler to catch more errors.

    [CUT]

    A secondary reason for introducing the new-style cast was that C-style casts are very hard to spot in a program. For example, you can't conveniently search for casts using an ordinary editor or word processor.

    [CUT]

    casts really are mostly avoidable in modern C++

    Also consider boost::numeric::converter / boost::numeric_cast that are safer alternatives (part of Boost.NumericConversion library).

    Eg

    #include <iostream>
    #include <boost/numeric/conversion/cast.hpp>
    
    int main()
    {
      using boost::numeric_cast;
    
      using boost::numeric::bad_numeric_cast;
      using boost::numeric::positive_overflow;
      using boost::numeric::negative_overflow;
    
      try
      {
        int i = 42;
        short s = numeric_cast<short>(i); // This conversion succeeds (is in range)
      }
      catch(negative_overflow &e)  { std::cout << e.what(); }
      catch(positive_overflow &e)  { std::cout << e.what(); }
    
      return 0;
    }
    

    In general for both implicit conversions and explicit conversions (through static_cast ) the lack of preservation of range makes conversions between numeric types error prone.

    numeric_cast detects loss of range when a numeric type is converted and throws an exception if the range cannot be preserved.


    Generally, these casting operators are classified under two major groups: specific casting operators and traditional casting operators . cplusplus.com explains it like this:

    ...In order to control these types of conversions between classes, we have four specific casting operators: dynamic_cast, reinterpret_cast, static_cast and const_cast. Their format is to follow the new type enclosed between angle-brackets (<>) and immediately after, the expression to be converted between parentheses.

    dynamic_cast <new_type> (expression)

    reinterpret_cast <new_type> (expression)

    static_cast <new_type> (expression)

    const_cast <new_type> (expression)

    The traditional type-casting equivalents to these expressions would be:

    (new_type) expression

    new_type (expression)

    but each one with its own special characteristics.

    While working on a task, we (nearly) all use specific casting . After considering the advises, it's up to you somehow.

    See the resource.

    链接地址: http://www.djcxy.com/p/28696.html

    上一篇: C ++铸造操作员和传统的C铸造操作员

    下一篇: C ++中用于在数字类型之间进行转换的最佳实践