Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Deduce function template argument

Reply
Thread Tools

Deduce function template argument

 
 
Ed
Guest
Posts: n/a
 
      08-14-2008
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!
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      08-14-2008
On Aug 14, 2:21 pm, "Alf P. Steinbach" <al...@start.no> wrote:
> * Ed:


> > 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.


Understandably. A float is not a vector.

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


> 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.)

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to deduce template argument implicitly? Nephi Immortal C++ 0 01-26-2013 04:14 AM
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
deduce element type of c-array template argument Maik C++ 2 12-03-2008 05:53 PM
template function argument deduce George2 C++ 0 03-11-2008 12:39 PM
"could not deduce template argument" error gretean@comcast.net C++ 7 11-10-2006 03:03 PM



Advertisments