运行时类型检查简单的模板函数 [英] Runtime type checking in a simple template function

查看:67
本文介绍了运行时类型检查简单的模板函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,


我有一个简单的模板将其参数转换为字符串:


模板< typename T>

string AtoStr(T t){

ostringstream sstrm;

sstrm<< t;

返回sstrm.str();

}


这对于stringstream来说很简单。但是,让我们说如果参数是一个

double 1.0或者int 1


返回的值是相同的1。没有小数点。我想

库正在优化这个库的输出。但是我需要做某事来对付这个:b / b

+如果参数的类型是int,那么它应该将它转换为

" 1"

+如果参数的类型是像1.0的双倍那么它应该是/ b $ b将其转换为1.0。


我不太确定输出操纵器是否可以帮助我。


祝你好运,

Dear all,

I have a simple template to convert its argument to a string:

template<typename T>
string AtoStr(T t){
ostringstream sstrm;
sstrm << t;
return sstrm.str();
}

This is simple with stringstream. But lets say if the argument is a
double 1.0 or an int 1

The returned value is the same "1" without decimal point. I guess the
library is optimizing the output on this one. But I need to do sth to
cope with this:

+ if the type of the argument is an int, then it should convert it to
"1"
+ if the type of the argument is a double like 1.0 then it should
convert it to "1.0".

I am not so sure if output manipulators can help me.

Best regards,

推荐答案

utab写道:
utab wrote:

亲爱的,


我有一个简单的模板,用于将其参数转换为字符串:


模板< typename T>

字符串AtoStr(T t){

ostringstream sstrm;

sstrm<< t;

返回sstrm.str();

}


这对于stringstream来说很简单。但是,让我们说如果参数是一个

double 1.0或者int 1


返回的值是相同的1。没有小数点。我想

库正在优化这个库的输出。但是我需要做某事来对付这个:b / b

+如果参数的类型是int,那么它应该将它转换为

" 1"

+如果参数的类型是像1.0的双倍那么它应该是/ b $ b将其转换为1.0。


我不太确定输出操纵器是否可以帮助我。
Dear all,

I have a simple template to convert its argument to a string:

template<typename T>
string AtoStr(T t){
ostringstream sstrm;
sstrm << t;
return sstrm.str();
}

This is simple with stringstream. But lets say if the argument is a
double 1.0 or an int 1

The returned value is the same "1" without decimal point. I guess the
library is optimizing the output on this one. But I need to do sth to
cope with this:

+ if the type of the argument is an int, then it should convert it to
"1"
+ if the type of the argument is a double like 1.0 then it should
convert it to "1.0".

I am not so sure if output manipulators can help me.



为什么这么麻烦。专业或超负荷。


V

-

请在通过电子邮件回复时删除资金'A'

我没有回复最热门的回复,请不要问

Why bother. Specialise or overload.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


为什么这么麻烦。专业化或超载。


嗯,我将阅读模板专业化。这是正确的


谢谢
Why bother. Specialise or overload.

Hmm, I will read on template specialization. Is that right

Thanks


在2007-03-18 17:58,utab写道:
On 2007-03-18 17:58, utab wrote:

亲爱的,


我有一个简单的模板将其参数转换为字符串:
Dear all,

I have a simple template to convert its argument to a string:



顺便说一句,关于帖子的主题:模板是编译的 -

时间因此运行时类型检查不会对你有什么好处因为所有的

应用程序运行时已做出决定。


-

Erik Wikstr?m

By the way, regarding the subject of your post: templates are compile-
time so runtime typechecking won''t do you any good since all the
decisions are already made when the app runs.

--
Erik Wikstr?m


这篇关于运行时类型检查简单的模板函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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