Archived

This topic is now archived and is closed to further replies.

Vlion

Forcing templates...

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I think you need to post more representative code, or at least elaborate more than you have. The base case of a templated function parameterized on return type defined in namespace scope is compiling just fine in both MSVC 6 and gcc 3.2.

Share this post


Link to post
Share on other sites
OK heres some code similar to whats going on.

class rec
{
//data. I say each variable has a number- a key
templateT getkey(unsigned int key);
};

template
T rec::getkey(unsigned int key)
{
/*
here a switch-case in which depending on the value of key, a different variable of possibly differing type is returned.
*/
}

int main()
{
//call getkey.
}
Is that enough code to get things rolling ?

~V''lion

Bugle4d

Share this post


Link to post
Share on other sites
No, it''s not enough.

The following compiles and runs fine on my system (although it complains about no return value)

int main()
{
//call getkey.
}

Please give an example which doesn''t compile but is exatcly what you want to achieve.

Share this post


Link to post
Share on other sites
I think that what you are trying to do is not possible. the return type is established at compile time, but you want to do it at runtime. What you will have to do is declare a base class that the function returns, with a set of virtual functions. Derive the required return types from this base class. This is assuming that you can give a common interface to all the types that you wish to return.

Think about what you are trying to do, if the function say returned an int, if you passed 0, and a float if you passed in 1, if you did not know the variable you were passing in (e.g. the user enters it), then how are you going to determine the variable to assign the return value to?

e.g. if var = 0 this is valid

int i = func(var);

but if var = 1, then the float would be converted to an int, not what you are expecting.

Share this post


Link to post
Share on other sites
Say you have a function:

template<class T>
T func(int param)
{ //....
}


then, forcing template...

int temp = func<int> (0);


you can't...

int temp = func(0);
int temp = func((int)0);


... since solving the T needs at least a parameter of type T.

[edited by - DerekSaw on March 4, 2003 8:40:27 PM]

[edited by - DerekSaw on March 4, 2003 8:41:06 PM]

Share this post


Link to post
Share on other sites