本文介绍了替换'浮动'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建一个自定义类''Float'',它会表现为
作为内置浮动类型的替代品?


正如在另一个帖子中所提到的,我曾经匆匆尝试过这个尝试

并且抓住一些浮点顽皮,但却被各种各样的b
地方所困扰。这样做有根本障碍吗?


谢谢,

GA

Should it be possible to create a custom class, ''Float'', which would behave
as a drop-in replacement for the builtin float type?

As mentioned in another thread, I once tried this in rather a hurry to try
and catch some floating point naughtiness, but was stumped in various
places. Is there a fundamental obstacle to doing this?

thanks,
G.A.

推荐答案




我不记得了。当使用类和内置类型时,有些人可能会声称传奇的

性能下降,但是

IMO并不总是如此。鉴于良好的编译器和实现

,你应该获得相同的性能。


V



Not that I can recall. Some would probably claim the legendary
performance decrease when using classes versus built-in types, but
IMO it''s not always true. Given a good compiler and implementation
you should get the same performance.

V





想到的一个基本障碍是可变功能。

将非POD类作为可变参数传递会产生未定义的行为。


否则,它似乎是可行的。


-

祝你好运,

Andrey Tarasevich



One fundamental obstacle that comes to mind is variadic functions.
Passing non-POD classes as variadic arguments results in udefined behavior.

Otherwise, it appears to be doable.

--
Best regards,
Andrey Tarasevich





我不明白,你是什么意思是直接替换?让我

想到:


typedef float Float;


你想拥有一个不能浮动的浮动包含某些值?东西

喜欢:


class SpecialFloat

{

private:


浮动数据;


public:


SpecialFloat& operator =(浮点输入)

{

if(输入!= 666)

{

data = input ;

}

}


运算符float const()

{

返回数据;

}

};

或者你可能正在寻找不同的东西...

-JKop



I don''t understand, what do you mean by "a drop-in replacment"? Made me
think of:

typedef float Float;

Do you want to have a float which cannot contain certain values? Something
like:

class SpecialFloat
{
private:

float data;

public:

SpecialFloat& operator=(float input)
{
if (input != 666)
{
data = input;
}
}

operator float const()
{
return data;
}
};
Or maybe you''re looking for something different altogether...
-JKop


这篇关于替换'浮动'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 14:59