模板元编程入门:顺序分支

顺序代码的编写方式

类型转换示例

为输入类型去掉引用并添加const

#include<iostream>
#include<type_traits>
struct fun_
{
	using RemRef = typename std::remove_reference<T>::type;
	//using type = typename std::add_const<RemRef>::type;
	constexpr static int value = sizeof(T) - S;
};

用别名模板简化

可以采用别名模板的方式简化元函数的书写:

template<typename T, unsigned S>
constexpr auto fun = fun_<T, S>::value;

于是可以直接这样写元函数:

std::cout << fun<double&, 4> << std::endl;

输出得到:

4

本文作者:WYFC4

本文链接:https://www.cnblogs.com/wyfc4/p/15867008.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   WYFC4  阅读(47)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.