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. 03 事务,连接池DBCP,C3P0,DBUtils

    事务 Transaction  其实指的一组操作,里面包含许多个单一的逻辑.只要有一个逻辑没有执行成功,那么都算失败. 所有的数据都回归到最初的状态(回滚) 事务的作用:为了确保逻辑的成功. 例子: ...

  2. C++数组排序

    #include<stdio.h> #include<stdlib.h> #include<windows.h> #define SIZE 5 //数组中元素的数量 ...

  3. LG4980 【模板】Polya定理

    题意 题目描述 给定一个$n$个点,$n$条边的环,有$n$种颜色,给每个顶点染色,问有多少种本质不同的染色方案,答案对$10^9+7$取模 注意本题的本质不同,定义为:只需要不能通过旋转与别的染色方 ...

  4. LG2634 [国家集训队]聪聪可可

    题意 题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)--遇到这种问题,一般情况下石头剪刀布就好了,可是 ...

  5. POJ1417 True Liars

    题意 Language:Default True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6392 Accep ...

  6. nginx负载均衡算法

    配置方式 NGINX配置负载均衡主要是在nginx.conf文件中里upstream模块 1.upstream模块应放于nginx.conf配置的http{}标签内2.upstream模块默认算法是w ...

  7. linux常见系统调用函数列表

    以下是Linux系统调用的一个列表,包含了大部分常用系统调用和由系统调用派生出的的函数.这可能是你在互联网上所能看到的唯一一篇中文注释的Linux系统调用列表,即使是简单的字母序英文列表,能做到这么完 ...

  8. centos7和linux防火墙配置入门

    linux部分 iptables -L    列出当前防火墙策略 iptables -F   清空防火墙策略 iptables -P INPUT  DROP   默认设置丢弃进来的流量包(-p指默认策 ...

  9. SqlDependency和SqlCacheDependency缓存的用法及具体步骤

    SqlDependency是outputcache网页缓存的一个参数,它的作用是指定缓存失效的数据库依赖项,可以具体到数据库和表. SqlDependency能解决什么问题? Asp.Net中的cac ...

  10. day 34 线程队列 线程池 协程 Greenlet \Gevent 模块

    1 线程的其他方法 threading.current_thread().getName()    查询当前线程对象的名字 threading.current_thread().ident      ...