using a class instead of a type

Started by
3 comments, last by SiCrane 15 years, 5 months ago
I was thinking, is there a way for me to have a class that works exactly like a type with no performance lost? here is an example

template<class T>
struct NativeTypeWrap
{
 typedef somePersonalType someType;
 T val;
 operator const T&() { return val; }
 T& operator =(const T& v) { val = v; return val; }
};
typedef NativeTypeWrap<short> u16;

^ that is untested but its something i like to do. The idea is it works exactly like short does in every way except i am attaching typedefs to it for overloading reasons. I have a class that doesnt want ints but specific size structs which can be ints but would need a typedef. I'd like the code to fail if i pass in an type that doesnt have those typedefs in it (so i know if i goofed up).
<SkilletAudio> Your framerate proves your lack of manhood
Advertisement
It sounds like what you're looking for is type traits.
Classes ARE types. You mean "... instead of a primitive", or perhaps "instead of a typedef".

And in C++, there is no likely performance loss for this kind of thing, certainly not in release mode.
Yeah, i want something used like a type_traits but this would hold different data.
What i mainly wanted to know is if something would bite me in the ass while doing this.

also, i need to implement each and every operator overload? or is there a way to only implement a few which would carry out the rest? maybe i can inherit something that already does this?
<SkilletAudio> Your framerate proves your lack of manhood
You could try using boost::operators.

This topic is closed to new replies.

Advertisement