explicit typedefs

Hey guys,

I'm looking for a way to make explicit type conversions between two "types" that are renames (typedefs) of the same base type. An example:

// Types
typedef double COST_TIME;
typedef double COST_MONEY;

COST_TIME ct = 50.0;
COST_MONEY cm = 60.0;

ct = cm; // Gramatically correct, conceptually bullshit. 

I know I could wrap COST_TIME into structs, but I'd have to redefine a ton of standard operations (multiply/divide by ints, doubles, etc.). It does give me close control over what is and what is not allowed (eg. sum/minus only with own type).

Are there other options? These are low level types so they are performance critical.
They are the same. It is implicit.

so, conceptually, it's not bullshit, it's part of the language.

typedef is basically not even compiled. Performance is as good as if you used double instead. It's just for readability, and, occasionally, OOP.
Last edited on
Perhaps you're looking for something like boost.units? http://www.boost.org/doc/libs/release/doc/html/boost_units.html
@IWishIKnew: Of course; I didn't mean "bullshit" as in "CPP done fucked up", but as "Both have an entirely different meaning to the reader/programmer".

@Cubbi: As always, your answer is exactly what I was looking for. Thanks!
This thread may also be of slight interest:
Oof, that hurt my head. I think I'll stick with Boost.units for now, but thanks for the link, L B!
Topic archived. No new replies allowed.