将模型转换为模板函数中的Int [英] Cast Chars To Int in Template Function

查看:197
本文介绍了将模型转换为模板函数中的Int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个打印数字的C ++模板函数。

I have a C++ template function which prints numbers.

它可以正常工作,除非我使用类型 char

It works fine for everything, except when I'm working with data of type char.

我想要 char c> int ,但是如果我在模板函数中显式地转换,那么我将失去对 float 类型的精度。

I'd like char to be printed as int, but if I cast this explicitly in the template function, then I will lose precision on my float types.

我想能够说:

template<class T> bob(T a){
  cout<<if_char_make_int(a)<<endl;
}

但我不知道该怎么做,

But I'm not sure how to do this, or if it is possible.

任何想法?

推荐答案

template<class T> void bob(T a){
    std::cout
      << typename boost::mpl::if_<boost::is_same<char, T>, int, T>::type(a)
      << std::endl;
}

这篇关于将模型转换为模板函数中的Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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