Forcing templates...

Started by
14 comments, last by Vlion 21 years, 1 month ago
Say you have a function

template
T func(int param)
{
    //....
}
 
Then somwhere in main() you say: int temp = func(0); Now depending on what param you put in, it will return a different type. My problem is that VC++ and g++ both freak out at a function of this type- they can''t find the right template. So how do I force a template? :-D ~V''lion Bugle4d
~V'lionBugle4d
Advertisement
try

int temp = func<int>(0);

no dice...
type int unexpected. *sour face*

Bugle4d
~V'lionBugle4d
Return types can''t specify template parameters. Usually you can get around that by making the return type one of the function parameters. So template T func(T parm). If you think about it the return type really should depend upon the parameters passed in. It isn''t always exactly the same type though. As an example multiplicate of two chars can result in a short. You can handle that with a template class that defines the return type. The template itself defines the default return type for types you didn''t anticipate, but will compile, and specializations of that template define the type when you want to override the default.

Even so I believe with your function even if the parameter was a template parameter you would still have to use either func(0) or func((int)0). That is one of the advantages of consts over defines and literals. They have a type. I''m not sure if 0 would be taken as char within which it fits, or long, or even bool for that matter.
Keys to success: Ability, ambition and opportunity.
dawg, LBW, thats a tangled thread of logic there.

ouch.
I do _not_ want to have to specify that.
Its for a database where I want any type to be in fields of a class, and then to return the data in a given field.

It makes the code look very ugly to have to do that.

n = func(0, tempint);

It would look nicer to do a
n = func(0, unsigned int);

Any other ideas ?


Bugle4d
~V'lionBugle4d
This code compiles fine in both MSVC .NET and g++ 3.2 .


  template <typename T>T func(int param) {  return T(param);}int main(void) {  int   temp  = func<int>(0);  float temp2 = func<float>(5);  return temp;}  


It could be the stuff in your //.... in the original post that is causing the problems.
int temp = func(0); 

It should work. However, VC++ 6 has a bug which, IIRC, prevents you from doing this. Upgrade to VC 7, or use LBW''s solution.

Cédric
quote:Original post by cedricl
int temp = func(0);  

It should work. However, VC++ 6 has a bug which, IIRC, prevents you from doing this. Upgrade to VC 7, or use LBW''s solution.


No, MSVC 6 will compile my above code as well. There is, however, a different defect in MSVC 6 that prevents explicit instantiation of template class member functions.
ach, VC6 problem...
I''ll check it under g++.

Thanks.

Bugle4d
~V'lionBugle4d
unfortunatly, g++ has a problem too with my code.

Any other ideas ?



Bugle4d
~V'lionBugle4d

This topic is closed to new replies.

Advertisement