Implementation of Narrow_cast in C + + programming language

Source: Internet
Author: User
Tags arithmetic

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.