In C + +, conversion of various numeric types is the main source of warnings during C + + compilation, but, many times, we need to use various numeric types, for example, we use one of the array to represent the size of the corresponding ordinal value, in this case, often involves a number of numeric types. Based on the recommendations in C + + programming language, the implementation of the C++14 version is given by using Narrow_cast for runtime security when converting numeric types.
//There is no implicit conversion from Source to TargetTemplate <TypeName Target, TypeName Source, TypeName= std::enable_if_t<!std::is_same<std::common_type_t<target, source>, std::d ecay_t<target>>:: Value>>inline Target narrow_cast (Source v) {Static_assert (!std::is_reference<target>::value,"The target couldn ' t be reference"); Static_assert (std::is_arithmetic<source>::value,"The parameter of narrow_cast should be arithmetic"); Static_assert (std::is_arithmetic<target>::value,"The return value of narrow_cast should be arithmetic"); //using Target_u = std::remove_reference_t<target>; //using Source_u = std::remove_reference_t<source>;Auto R= static_cast<target>(v); if(Static_cast<source> (r)! =v)ThrowStd::runtime_error ("narrow_cast<> () failed"); returnR;}//There is implicit conversion from Source to TargetTemplate <TypeName Target, TypeName Source, TypeName= std::enable_if_t<Std::is_same<std::common_type_t<target, Source>, std::d ecay_t<target>>::value>>inline constexpr std::remove_reference_t<Source>Narrow_cast (Source v) {Static_assert (!std::is_reference<target>::value,"The target couldn ' t be reference"); Static_assert (std::is_arithmetic<source>::value,"The parameter of narrow_cast should be arithmetic"); Static_assert (std::is_arithmetic<target>::value,"The return value of narrow_cast should be arithmetic"); returnStatic_cast<target>(v);}
The following is a simple test case written using catch:
#defineCatch_config_main#include"catch.hpp"#include<cmath>Test_case ("Test Narrow_cast","[Narrow_cast]"){ inti =Ten; Long Longj = the; Long Long& k =J; REQUIRE (Narrow_cast<Long> (k) = = the); REQUIRE (Narrow_cast<Long> (i) = =Ten); Long LongVery_big = POW (Ten, A); BOOLException =false; Try{narrow_cast<Long> (very_big) = =Very_big; } Catch(Conststd::runtime_error&error) {Exception=true; } REQUIRE (Exception); //REQUIRE (narrow_cast<long&> (k) = = 15); //REQUIRE (narrow_cast<long&> (i) = = ten);}
The test shows that when the type of conversion can be accommodated, the narrow_cast can function normally, and if the Narrow_cast conversion value is not the same as the original value, the Runtime_error exception will be thrown.
Implementation of Narrow_cast in C + + programming language