Ambiguous template function, best way to solve it...

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

Hi,
I've got a template function

move_by( rect<T>&, T x, T y) being called as

rect<float> r;
move_by( r, 3.0, 4.0 );

However, i'm getting stupid ambiguities when I use
floating point numbers.

event_handler.cpp(43) : error C2782: 'void ' : template parameter 'T' is
ambiguous
could be 'double'
or 'float'

This message is referring to the x,y arguments.

Do I really have to specialise for these types? It
seems rediculous and defeats the purpose of having templates
to begin with.

Any ideas?

Thanks,
Mark.
 
Hi,
I've got a template function

move_by( rect<T>&, T x, T y) being called as

rect<float> r;
move_by( r, 3.0, 4.0 );

However, i'm getting stupid ambiguities when I use
floating point numbers.

event_handler.cpp(43) : error C2782: 'void ' : template parameter 'T' is
ambiguous
could be 'double'
or 'float'

This message is referring to the x,y arguments.

Do I really have to specialise for these types? It
seems rediculous and defeats the purpose of having templates
to begin with.

Any ideas?

Your template says that it takes a rect<T> and T's. You are passing it
a rect<float> and two doubles, so unsurprisingly it doesn't know which
one you want. You could do:

//all floats
rect<float> r;
move_by( r, 3.0f, 4.0f);

or
//all doubles
rect<double> r
move_by( r, 3.0, 4.0);

//allow mix of float and double.
or change move_by to
template <class T, class U>
void move_by(rect<T>&, U x, U y);

Tom
 
Hi Tom
I had forgotten that the f suffix existed :)
Works now! Still considering if I need the
dual type version...

Thanks very much,
Mark.
 
Back
Top