Sign in to follow this  
AcidZombie24

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this