模板专业化一组类型 [英] template specialization for a set of types

查看:163
本文介绍了模板专业化一组类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个快速问题,如何专门化模板的一组数据类型。例如

i have a quick question on how do I specialize a template for a set of datatypes. for example,

template<typename T>
inline T getRatio(T numer, T denom){
    return (numer/denom);
}

我想要使用int,long,double,float以专门用于此组数据类型。因此如果用户使用'char'类型尝试此函数,编译器将抛出错误。
如果这是一个dup让我知道。
感谢 只要三种数据类型 long 感谢

I want this to work with int, long, double, float so I want to specialize it for this set of datatypes. so that if the user tries this function with a 'char' type, the compiler would throw error. if this is a dup let me know. thanks

推荐答案

double float 是相关的,他们不需要额外的通用版本,只是拒绝 template 并为 long double float

As only the three data types long, double and float are relevant and their is no need for an additional generic version, just reject the template and provide three functions for long, double and float.

这篇关于模板专业化一组类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆