C++ Programming Language中的narrow_cast实现
在C++中,各种数值类型的转化是C++编译过程中警告的主要来源,但是,很多时候,我们需要使用各种数值类型,例如我们用数组的某一位表示大小为对应序号的值,这种情况下,经常会涉及多种数值类型。根据C++ Programming Language中的建议,在数值类型转换时,使用narrow_cast来实现运行时安全,这里给出C++14版本的实现。
// there is no implicit conversion from Source to Target
template <typename Target, typename Source,
typename = std::enable_if_t<
!std::is_same<std::common_type_t<Target, Source>, std::decay_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)
throw std::runtime_error("narrow_cast<>() failed");
return r;
} // there is implicit conversion from Source to Target
template <typename Target, typename Source,
typename = std::enable_if_t<
std::is_same<std::common_type_t<Target, Source>, std::decay_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"); return static_cast<Target>(v);
}
下面给出,使用Catch写的简单测试用例:
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include <cmath> TEST_CASE("Test narrow_cast", "[narrow_cast]")
{
int i = ;
long long j = ;
long long& k = j;
REQUIRE(narrow_cast<long>(k) == );
REQUIRE(narrow_cast<long>(i) == );
long long very_big = pow(, );
bool exception = false;
try
{
narrow_cast<long>(very_big) == very_big;
}
catch (const std::runtime_error& error)
{
exception = true;
}
REQUIRE(exception);
//REQUIRE(narrow_cast<long&>(k) == 15);
//REQUIRE(narrow_cast<long&>(i) == 10);
}
测试可知,在转化的类型可以容纳时,narrow_cast可以正常运行,如果narrow_cast转化后的值与原值不同时,会抛出runtime_error的异常。
C++ Programming Language中的narrow_cast实现的更多相关文章
- C++ Programming Language中的Calculator源代码
C++ Programming Language 4th中的Calculator源代码整理,因为在C++ Programming Language中,涉及了很多文件位置之类的变化,所以,这里只是其中的 ...
- 编程提取字符串"Java is a programming language"中的各个单词,并打印输出。
import java.lang.String; import java.util.StringTokenizer; public class StringGetWord{ /* 编程提取字符串&qu ...
- iOS Swift-元组tuples(The Swift Programming Language)
iOS Swift-元组tuples(The Swift Programming Language) 什么是元组? 元组(tuples)是把多个值组合成一个复合值,元组内的值可以使任意类型,并不要求是 ...
- iOS Swift-控制流(The Swift Programming Language)
iOS Swift-控制流(The Swift Programming Language) for-in 在Swift中for循环我们可以省略传统oc笨拙的条件和循环变量的括号,但是语句体的大括号使我 ...
- The Swift Programming Language 中文翻译版(个人翻新随时跟新)
The Swift Programming Language --lkvt 本人在2014年6月3日(北京时间)凌晨起来通过网络观看2014年WWDC 苹果公司的发布会有iOS8以及OS X 10.1 ...
- [iOS翻译]《The Swift Programming Language》系列:Welcome to Swift-01
注:CocoaChina翻译小组已着手此书及相关资料的翻译,楼主也加入了,多人协作后的完整译本将很快让大家看到. 翻译群:291864979,想加入的同学请进此群哦.(本系列不再更新,但协作翻译的进度 ...
- the C programming language 阅读笔记1
读了一遍著名的<the C programming language>,果然如听说的一样,讲解基础透彻,案例简单典型,确实自己C语言还有很多细节点不是很清楚. 总结一下阅读的收获(部分原书 ...
- Introduction to OOC Programming Language
Introduction to OOC Programming Language 文/akisann @ cnblogs.com / zhaihj @ github.com 本文同时发布在github ...
- 不忘初心 --- 重读<<The C Programming Language>>
这篇文章应该发布在好几年前,2011年计算机界大师Dennis Ritchie仙逝,那时对大师的映象还停留在大一刚学编程时:Unix的合作开发者,C语言的发明人.通过网上的纪念文章<<Un ...
随机推荐
- 使用mint-ui中弹框组件与原生弹框阻止父页面不滑动方法
1,使用mint-ui框架中<mt-popup></mt-popup>,在组件中加入 lockScroll="true" 阻止父页面不滑动. 2,原生弹框中 ...
- Vue.js 3.0 新特性预览
总结起来,Vue 3 以下方面值得我们期待 : 更快 更小 更易于维护 更多的原生支持 更易于开发使用 完整的PPT:docs.google.com/presentatio… Evan 和 Vue 团 ...
- Linux进程间通信机制
Linux支持管道.信号.unix system V三种IPC(Inter-Process-Communication)机制.以下分别对三种机制加以简单介绍. 一.信号机制: 信号又称作软中断,用来通 ...
- ubuntu16.04中设置python3
执行: sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100 sudo update-alter ...
- Win10系列:C#应用控件进阶3
椭圆 若要绘制椭圆需要用到Ellipse元素,通过指定Ellipse元素的Width和Height属性值来确定椭圆的大小,其中Width指椭圆在X轴的宽度,Height指椭圆在Y轴的高度,若X轴和Y轴 ...
- learning makefile grammar
- jq demo 点击选中元素左右移动
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- JDBC遇到向ORACLE数据库表执行插入操作时,报错“列在此处不允许”
此异常的原因在于,编写的SQL语句,其中的变量已经成了字符串,这种情况对数值类数据没有影响,但是对字符串类数据有影响,应该在SQL语句中的字符串类变量左右两边加上单引号.如下:
- IIS 8.5详细错误
把网站部署到IIS后报错,我错的原因是文件夹内没有设置默认文档,计算机不知道运行哪一个文件,所以报错. 方法:(1)在IIS目录下找到默认文档,双击,点击添加,手写 自己的 启动文件 (2)启动自己的 ...
- 团队项目(MVP------新能源无线充电管理网站)(总结)
经过了几个月的学习时间与团队的磨合以及一系列的困难之后,我们mvp小组一起完成了这个项目,内心也是十分激动和有成就感的.其实一开始基础并不好,很多都不知道,但是通过在慕课网上的学习以及老师严厉地督促下 ...