Question:
We do have a template based class, that provides some functionality around a primitive type (e.g. serializing, deserializing, ...)
template <typename PRIM_TYPE> class WrappedPrimitive
{
public:
PRIM_TYPE v;
//lots of other methods, including serialization and deserialization
PRIM_TYPE & operator= (const PRIM_TYPE & value) { v = value; return v; }
operator PRIM_TYPE() const { return v; }
};
typedef WrappedPrimitive<float> WrappedPrimitiveFloat;
Let's say I have a function
void maninpulateFloat(float *f) {*f = 5.0f;}
Is there any way to add some class function, such that it returns the address of the v
member when casting from *WrappedPrimitiveFloat
to *float
. (Or in general from *WrappedPrimitive<PRIM_TYPE>
to *PRIM_TYPE
?
WrappedPrimitiveFloat myfloat;
manipulateFloat(&myfloat.v) // This works
manipulateFloat(&myfloat) // Is there any way to make this work?
Background:
I know it is maybe not a good idea, but the reason I'd like it is because we ported the code to Linux, where myfloat will actually be a float and not some wrapped value. Right now, we need to distinguish these places with
#ifdef _WIN32
manipulateFloat(&myfloat.v)
#else
manipulateFloat(&myfloat)
#endif
which is also really ugly. Alternatively, I'd be happy with a line of code that works on both Windows/Linux.
What I have tried
It is possible to overload the operator& function,
PRIM_TYPE * operator& () {return &v;}
but according to this post Overloading unary operator & it is not the best idea, as using & will always return a PRIM_TYPE*
.