 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Joseph Turian Guest
|
Posted: Sun Nov 26, 2006 10:11 am Post subject: Strongly type doubles? |
|
|
I want to create several types of double, e.g. D1 and D2.
I want strong typing, such that a function defined for D1 will not
accept a D2 argument.
One solution is:
struct D1 {
double d;
};
However, the usage is cumbersome (I have to access member variable d),
I don't have any operators defined, and object creation is more
expensive than for native types (right?).
Can someone propose an alternate solution that enforces the type safety
I require?
Thanks,
Joseph |
|
| Back to top |
|
 |
kwikius Guest
|
Posted: Sun Nov 26, 2006 10:11 am Post subject: Re: Strongly type doubles? |
|
|
Joseph Turian wrote:
| Quote: | I want to create several types of double, e.g. D1 and D2.
I want strong typing, such that a function defined for D1 will not
accept a D2 argument.
One solution is:
struct D1 {
double d;
};
However, the usage is cumbersome (I have to access member variable d),
I don't have any operators defined, and object creation is more
expensive than for native types (right?).
Can someone propose an alternate solution that enforces the type safety
I require?
|
It is perfectly possible to make your own types that act like doubles,
but with whatever semantics you want to design in. Its not trivial
though to get all the details the way you want them to be.
The link here is to my code for a physical quantity type, but the
underlying principle involved is the same for a strongly typed double:
http://quan.sourceforge.net/index.html.
For variables the assembly code is indistinguishable from that using
doubles in most cases (using VC8.0 with a very good optimiser), however
the code for literal constants is better optimised for inbuilt types so
you do seem to lose performance there.
regards
Andy Little |
|
| Back to top |
|
 |
Markus Svilans Guest
|
Posted: Sun Nov 26, 2006 10:11 am Post subject: Re: Strongly type doubles? |
|
|
Joseph Turian wrote:
| Quote: | I want to create several types of double, e.g. D1 and D2.
I want strong typing, such that a function defined for D1 will not
accept a D2 argument.
One solution is:
struct D1 {
double d;
};
However, the usage is cumbersome (I have to access member variable d),
I don't have any operators defined, and object creation is more
expensive than for native types (right?).
Can someone propose an alternate solution that enforces the type safety
I require?
Thanks,
Joseph
|
If you use the structs, you could define conversion operators for D1
and D2:
struct D1 {
double d;
operator double () { return d; }
};
Then you will be able to use them in mathematical expressions because
the conversion to double will take place automatically:
D1 a, b;
a.d = 15;
b.d = 25;
double c = a * b;
You will also be able to have functions that accept D1 objects, but not
raw doubles or D2 objects.
If I'm not mistaken, the compiler should also inline the conversions,
so you will not have any performance penalty for using them, instead of
regular doubles.
Regards,
Markus. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|