c++ - How to get float value from object wrapped float in the sam way? -
pi'm trying wrap float, int, etc primitves value sparam class. have problem getting values struct. want use struct use example floats.
template<class t> struct sparam { t value; sparam() { } operator t() const { return value; } }; sparam<float> a; a.value = 4; printf("%f", a); //<--this don't print a.value
the problem using "naked" sparam<float> a
printf
(or other function takes variable number of arguments, matter) compiler not know want pass float
. why type-specific conversion not applied. if call function specific parameter type, say
void printfloat(float f) { printf("%f", f); } ... sparam<float> a; a.value = 4; printfloat(a);
then compiler figure out signature of printfloat
conversion float
needs applied, , work expected. of course can apply conversion manually:
printf("%f", (float)a);
Comments
Post a Comment