Overloaded member functions for a particular template specialisation?

You need to specialize the class, like this: template class tPoint{ int x; int y; public: void Set(int ix, int iy){x=ix;y=iy;} void Set(float ix, float iy){x = ix+0.5; y = iy+0.5;} }.

I'd specialize the float version instead of all the non float one :-) – AProgrammer Feb 26 at 17:55 @AProgrammer, the OP only mentioned wanting to specialize for tPoint, but I agree if he wants the second Set for more than one class. – Karl Bielefeldt Feb 26 at 18:05.

The issue you're facing has to do with T standing either for int or float. If you look at the template class definition, you'll remark that the same T that appears after typename in the template part also appears as parameter to the Set method. This means that when you speak of Point then there is only one Set method defined, which takes two int.

And each different Point will have its own Set(T,T) method. If you wish for a different Set method, then you need to declare a template Set within the template class, this is done like so: template class Point { public: template void Set(Num x, Num y); }; Note how I had to chose a different template parameter name. In order to solve your problem, you could introduce another method, for float, but then you'd have to have another for double, and long double... it's soon going to become difficult.

The simplest solution, is to go bruteforce: template template void Point::Set(Num x, Num y) { this->x = long double(x) + 0.5; this->y = long double(y) + 0.5; } For int and al, it's mostly useless but works. For floating points, we use the bigger floating point type available to avoid losing precision, and then perform the rounding. Obviously, it does not quite work if suddenly we want a Point, therefore we need a smarter solution, based on type traits.

The class std::numeric_limits has a is_integer which precises whether or not we are dealing with an integral type. Template template void Point::Set(Num x, Num y) { if (std::numeric_limits::is_integer &&! Std::numeric_limits::is_integer) { this->x = x + 0.5; this->y = y + 0.5; } else { this->x = x; this->y = y; } } } I know it seems stupid to use a if for something that could be determined at compile-time... but don't worry, the compiler is smart enough to do figure it out at compile-time and optimize away the if and the unused branch altogether ;).

Thanks, it's more complex than what I'd expect but your post has some interesing points for a novice in template specialization – Juan Feb 26 at 19:46.

Use boost enable_if to prevent the float version when instanciated with float.

Your best bet is: templateclass tPoint{ T x; T y; public: void Set(T ix, T iy) { set_impl(boost::type(), x, y); } private: void set_impl(boost::type, float ...); template void set_impl(boost::type, T ...); }.

Try not to provide the default implementation of Set() inside the class. That way, it's easier to specialize for float.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions