Jump to content
  • Advertisement

Archived

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

Vlion

Forcing templates...

This topic is 5681 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!