模板元编程入门:顺序分支
顺序代码的编写方式
类型转换示例
为输入类型去掉引用并添加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 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步