# using a class instead of a type

## Recommended Posts

AcidZombie24    100
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).

##### Share on other sites
Gage64    1235
It sounds like what you're looking for is type traits.

##### Share on other sites
Zahlman    1682
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.

##### Share on other sites
AcidZombie24    100
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?

##### Share on other sites
SiCrane    11839
You could try using boost::operators.