SFINAE(Substitution failure is not an error),是C++11以来推出的一个重要概念,这里,只是简单举一个例子,可能会有人需要。

// 添加 scalar numeric conversion function,实现源自 C++ programming language(4th)
// 用来防止使用static转换的时候,值发生改变 // there is no implicit conversion from Source to Target
template <typename Target, typename Source,
typename = std::enable_if_t<!std::is_reference_v<Target> &&
!std::is_same_v<std::common_type_t<std::decay_t<Target>, std::decay_t<Source>>, std::decay_t<Target>>, int>>
inline Target narrow_cast(Source v)
{
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_reference_v<Target> &&
std::is_same_v<std::common_type_t<std::decay_t<Target>, std::decay_t<Source>>, std::decay_t<Target>>, int>>
inline constexpr std::remove_reference_t<Source> narrow_cast(Source v)
{
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 std::remove_reference_t<Source>(v);
}

SFINAE简单实例的更多相关文章

  1. Hibernate(二)__简单实例入门

    首先我们进一步理解什么是对象关系映射模型? 它将对数据库中数据的处理转化为对对象的处理.如下图所示: 入门简单实例: hiberante 可以用在 j2se 项目,也可以用在 j2ee (web项目中 ...

  2. 最新 Eclipse IDE下的Spring框架配置及简单实例

    前段时间开始着手学习Spring框架,又是买书又是看视频找教程的,可是鲜有介绍如何配置Spring+Eclipse的方法,现在将我的成功经验分享给大家. 本文的一些源代码来源于码农教程:http:// ...

  3. 修改js confirm alert 提示框文字的简单实例

    修改js confirm alert 提示框文字的简单实例: <!DOCTYPE html> <html> <head lang="en"> & ...

  4. 利用navicat创建存储过程、触发器和使用游标的简单实例

    利用navicat创建存储过程.触发器和使用游标的简单实例 标签: navicat存储过程触发器mysql游标 2013-08-03 21:34 15516人阅读 评论(1) 收藏 举报  分类: 数 ...

  5. 【转】Android Https服务器端和客户端简单实例

    转载地址:http://blog.csdn.net/gf771115/article/details/7827233 AndroidHttps服务器端和客户端简单实例 工具介绍 Eclipse3.7 ...

  6. Centos7的安装、Docker1.12.3的安装,以及Docker Swarm集群的简单实例

    目录 [TOC] 1.环境准备 ​ 本文中的案例会有四台机器,他们的Host和IP地址如下 c1 -> 10.0.0.31 c2 -> 10.0.0.32 c3 -> 10.0.0. ...

  7. vue路由的简单实例

    vue2.0 和 vue1.0 路由的语法还是有点稍微的差别,下面介绍一下vue-router 2的简单实例: <!DOCTYPE html> <html lang="en ...

  8. Flume概述和简单实例

    Flume概述 Flume是一个分布式.可靠.和高可用的海量日志采集.聚合和传输的系统.支持在日志系统中定制各类数据发送方,用于收集数据;同时,Flume提供对数据进行简单处理,并写到各种数据接受方( ...

  9. jsoup解析HTML及简单实例

    jsoup 中文参考文献    http://www.open-open.com/jsoup/ 本文将利用jsoup,简单实现网络抓取的功能,并给出一个小实例,该实例效果为:获取作者本人在博客园写的所 ...

随机推荐

  1. ISCC的 Misc——WP

    比赛已经结束了,自己做出来的题也不是很多,跟大家分享一下 Misc 第一题:What is that? 下载链接; 打开 解压 是一个图片 因为分值很少所以题和简单 观察图片是一个向下指的手 说明fl ...

  2. python基础-函数基本特性和用法

    函数: 初中数学函数定义:一般的,在一个变化过程中,如果有两个变量x和y,并且对于x的每一个确定的值,y都有唯一确定的值与其对应,那么我们就把x称为自变量,把y称为因变量,y是x的函数.自变量x的取值 ...

  3. 【BZOJ4521】【CQOI2016】手机号码

    感觉数位dp好恶心…… 原题: 人们选择手机号码时都希望号码好记.吉利.比如号码中含有几位相邻的相同数字.不含谐音不 吉利的数字等.手机运营商在发行新号码时也会考虑这些因素,从号段中选取含有某些特征的 ...

  4. SPOJ NSUBSTR Substrings

    题意 dt { font-weight: bold; margin-top: 20px; padding-left: 35px; } dd { box-shadow: 3px 3px 6px #888 ...

  5. MySQL Cursor Demo

    -- 使用cursor的demo -- ==============================## -- 删除存储过程 DROP PROCEDURE USP_TestCursor; DELIMI ...

  6. sqler sql 转rest api 的docker image

    最新sqler 又发布了一个版本,同时官方文档也更新,对于数据库的连接有了详细的说明 Dockerfile 为了方便以及减少大小,使用多阶段构建,同时都通过环境变量运行 FROM alpine:lat ...

  7. lch 儿童围棋课堂 初级篇2 (李昌镐 著)

    第1章 吃子 第2章 死活:实战演练 第3章 劫争 第4章 布局:定式篇 第5章 布局:三线,四线和大场 第6章 布局:布局类型与形势判断 第7章 中盘:分断 第8章 官子:价值的计算 第9章 对杀技 ...

  8. ASP.NET MVC中常用的ActionResult类型

    常见的ActionResult 1.ViewResult 表示一个视图结果,它根据视图模板产生应答内容.对应得Controller方法为View. 2.PartialViewResult 表示一个部分 ...

  9. ReportViewer的使用总结

    1.换行符:chr(13)&chr(10) 2.时间字符串格式化:  =IIF(Trim(Fields!business_time.Value).Length=6,   Left(Trim(F ...

  10. CloudStack学习-2

    环境准备 这次实验主要是CloudStack结合glusterfs. 两台宿主机,做gluster复制卷 VmWare添加一台和agent1配置一样的机器 系统版本:centos6.6 x86_64 ...