没有rtti的模板定义中的float / double相等 [英] float/double equality in template definition without rtti

查看:90
本文介绍了没有rtti的模板定义中的float / double相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello newsgroup,


让'我说我有一个类似
的功能

模板< class Type>

inline bool areEqual(const Type& a,const Type& b)

{

return(a == b);

}


这可能会导致警告,说两个float或double值的(in)相等是不安全的。所以我想在不使用RTTI的情况下将它们视为异常,并且不为每种可能的类型定义单独的函数。那可能吗 ?有没有关于如何实现这种功能的想法?


提前致谢,


Alex

解决方案

Alexander Block写道:

你好新闻组,

让我说我有一个像

模板<类类型>
内联bool areEqual(const类型& a,const类型& b)
{
return(a == b);
}

这可能会导致警告,说两个浮点数相等(或)两个值不安全。因此,我希望将它们视为
异常而不使用RTTI,也不为每种可能的类型定义单独的函数。那可能吗 ?是否有任何关于如何实现这种功能的想法?




您可以使用模板专业化。试试这个(在上面的模板

定义之后):


模板<>

内联bool areEqual< float>(const float& a,const float& b)

{

return abs(a - b)< some_epsilon_value; //或者你需要的任何东西

}


顺便说一句,如果你想要内联,你应该考虑功能

对象而不是常规功能。如果你将比较函数

作为参数传递给另一个函数,将传递一个指向它的指针,它将通过一个指针调用

,这排除了任何机会for

inline。


blockquote>

Alexander Block写道:


Hello newsgroup,

让我说我有一个类似

模板的函数< class Type>
内联bool areEqual(const Type& a,const Type& b)
{
返回(a == b);
}
这可能会导致警告,说两个float或double值的(in)相等是不安全。所以我想在不使用RTTI的情况下将它们视为异常,并且不为每种可能的类型定义单独的函数。那可能吗 ?有没有关于如何实现这种功能的想法?




转动#


(*& @(一些编译器作者关于什么可能是错误的概念通常与实际上正确无关的概念很少。


- -


Pete Becker

Dinkumware,Ltd。( http://www.dinkumware.com


Hello newsgroup,

let''s say I have a function like

template<class Type>
inline bool areEqual(const Type &a, const Type &b)
{
return ( a == b );
}

This could result in a warning, saying the (in)equality of two float or double values is not secure. So I want to treat them as an exception without using RTTI and without defining a seperate function for each possible type. Is that possible ? Are there any ideas of how to realize such kind of functionality ?

Thanks in advance,

Alex

解决方案

Alexander Block wrote:

Hello newsgroup,

let''s say I have a function like

template<class Type>
inline bool areEqual(const Type &a, const Type &b)
{
return ( a == b );
}

This could result in a warning, saying the (in)equality of two float
or double values is not secure. So I want to treat them as an
exception without using RTTI and without defining a seperate function
for each possible type. Is that possible ? Are there any ideas of how
to realize such kind of functionality ?



You can use template specialization. Try this (after the above template
definition):

template<>
inline bool areEqual<float>(const float &a, const float &b)
{
return abs(a - b) < some_epsilon_value; // or whatever you need
}

Btw, if you want this to be inlined, you should consider function
objects instead of regular functions. If you pass your compare-function
as parameter to another function, a pointer to it will be passed and it
will be called through a pointer, which rules out any chance for
inlining.


Alexander Block wrote:


Hello newsgroup,

let''s say I have a function like

template<class Type>
inline bool areEqual(const Type &a, const Type &b)
{
return ( a == b );
}

This could result in a warning, saying the (in)equality of two float or double values is not secure. So I want to treat them as an exception without using RTTI and without defining a seperate function for each possible type. Is that possible ? Are there any ideas of how to realize such kind of functionality ?



Turn of the #


(*&@( warning. Some compiler writer''s notion of what might
be a mistake often has very little to do with what''s actually correct.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)


这篇关于没有rtti的模板定义中的float / double相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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