Difference between template class and function overloading




















Function templates? Carlos Moreno wrote: WittyGuy wrote: Hi, What is the major difference between function overloading and function templates? Wg No. As Carlos said, this stinks of homework.

But it's a pretty bad question, if it is -- far to general for an intelligent student to know what is really being asked. I would start by saying that the two aren't really related; they work on completely different levels. Basically, function overloading allows the definition of more than one function with the same name, and provides a means of choosing between the functions based on parameter matching.

Template functions are a means of specifying to the compiler how to create new functions, based on the instantiation type. A template function itself isn't a function, and doesn't intervene in function overloading. Functions generated from the template instantiations however, behave like normal functions, and do participate in overload resolution. Basically, when the compiler encounters a function call, it will first look up the name, according to the scope of the call.

All functions with the name, in the same scope, will be taken as the overload set. If the name corresponds to a template function, the compiler will, if possible, instantiate that function, and add the generated function to the overload set. And of course, this is done for all applicable template functions. It then applies the rules of overload generation to the set, to choose which function it calls. IMHO, the keys to understanding this are 1 a function template is not a function, it is only a recepe for the compiler to produce a function, 2 the rules for deciding whether to produce a function from a function template, and which function to produce, are independant of the rules for overload resolution, and 3 the decisions concerning the template function are taken before starting overload resolution.

Finally, once the overload set has been constructed, the fact that a function is the instantiation of a template does play a role in overload resolution. Basically, if two functions are equally good matches, and one is the instantiation of a template, and the other isn't, the one that isn't wins, and if two functions are equally good matches, and both are instantiations of a template function, then the one whose template is the most specialized wins. Greg wrote: WittyGuy wrote: What is the major difference between function overloading and function templates?

The way I understand it, that's not quite what happens although I think it was with several pre-standard compilers. The decision to try to instantiate the template function takes place independantly of overloading. If two functions are otherwise equally good matches, and one is the result of instantiating a template function, and the other not, overload resolution will choose the one that isn't.

Note, however, that the results of instantiating a template function will often be an exact match; if the non template function requires some non-trivial conversions, the template instantiation will be preferred.

This is, I think, a significant difference between the standard and most pre-standard compilers. Difference largely attenuated by the fact that most pre-standard compilers had so many problems with templates that you only did the simplest things with them, so the problem cases don't occur. Once overload resolution has finished, if the chosen function is an instance of a template, the compiler will instantiate the definition.

I mean, why would you need the other version, once you have this one? Tokyo Tomy. All posters write the differences mainly from the viewpoint of matching order so far.

I would like to write one of the differences from the viewpoint of call convention. The only thing that behaves differently is the match function. Is there a "more efficient" way of doing this? Could this be done with function overloading of the match function?

If it can be overloaded, I would appreciate ideas on how. I've tried several variations of overloading and failed miserably. I started to apply PigBen's answer and was able to get it to work with my problem as stated above. Then I tried it on my actual code and realized that I oversimplified my problem. Active 1 year, 11 months ago. Viewed 32k times. Or, are there good reasons to use one instead of the other?

Improve this question. Fantastic Mr Fox I think that you don't get what are templates and overloaded functions. Basically these are two different topics. You can have template of class or function but still you may need to overload it. Blood i corrected my question's title. Add a comment. Active Oldest Votes. Improve this answer. So, basically should we use overloading when we want to do different things "within the same function"; double comas because it is not actually the same function based on the types that we pass the function?

Just like Mike Corcoran below answer says, right? Puppy Puppy k 34 34 gold badges silver badges bronze badges. But they weren't around when overloading was introduced. Mike Corcoran Mike Corcoran 13k 4 4 gold badges 32 32 silver badges 48 48 bronze badges.

But still that can be achieved with function templates too. Template makes the program bigger. Notify me of new comments via email. Notify me of new posts via email.

Skip to content - - Josh. Function overloading allows us to have multiple functions with the same identifier in the same scope as follows If these two functions had the same implementation, then given that the parameters vary only by type, we could of course use a template. Share this: Twitter Facebook. Like this: Like Loading Published by Josh.

Leave a Reply Cancel reply Enter your comment here



0コメント

  • 1000 / 1000