Deduce function template argument

E

Ed

Hi, guys,
I met a issue that compiler can not deduce the template argument type
from my code.


template <typename Precision = float>
struct Vector
{
typedef Precision scalar;

Precision x;

Vector(): x()
{
};

Vector(Precision r)
{
x = r;
}

template <typename P>
Vector(Vector<P>& r)
{
x = (Precision) r.x;
}

template<typename P>
operator P()
{
return (P)x;
};
};

template <typename P>
void Hello(Vector<P> v)
{
};

// use of the template
int main()
{
Hello(1.0f); //Not work

float f;
Hello(f); //Not work

return 0;

}


I want compiler to understand Hello(1.0f) is
Hello( Vector<float>(1.0f) ).
But compiler can't find this.

Hoe can I make implicit constructor of Vector when the argument is
float?

Thanks!
 
J

James Kanze


Understandably. A float is not a vector.
Templated arguments must match *exactly*.

To tell the truth, this has nothing to do with templates. Even
if he had a function "void Hello( Vector<float> v )", it
wouldn't work. (Or maybe it would---he didn't show us the
definition for Vector. But I can't imagine anything called
Vector with a conversion constructor which would take a float.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top