views:

96

answers:

3

I know that Function Templates are used so as to make the functions portable and so that they could be used with any data types.

Also Explicit Specialization of templates is done if we have a more efficient implementation for a specific data type.

But then instead of Explicit Specialization we could also just code a Nontemplate Function which could be called from main . This would save us some processing time as the compiler would locate Nontemplate Functions faster than Explicitly Specialized Templated Functions which would in turn be better in terms of efficiency.

So why do we use Explicit Specialization when we have the alternative of just calling Nontemplate Functions?

Please correct me If I'm wrong!

Edit 1: I was told by my professor that whenever we make function templates and call the function from main ,the compiler first looks for a templated function and if its not able to locate that,then it searches for a function template from which it in turn makes a templated function and then calls for it.

+1  A: 

For reasons of uniformity. The person using the API just calls methods with particular arguments, some get the generic function some get explicitly specialised functions - the client need neither know nor care which they use.

djna
You should probably also point out that he is, in fact, wrong: global functions would not be any more efficient either.
Nick
The consumer of the API doesn't care, though. Functions overload, and a function and a function template with the same name can both be found during name resolution and can both be considered as candidates during overload resolution. There are few times that the consumer of the API would care, for example if the function template is explicitly used (e.g., `f<>(x)` instead of `f(x)`) or if certain template arguments cannot be deduced from the function arguments (in which case the consumer has to decide what to do anyway).
James McNellis
+4  A: 

This would save us some processing time as the compiler would locate Global Functions faster than Explicitly Specialized Templated Functions which would in turn be better in terms of efficiency.

Why would the compiler find a nontemplate function faster than a function template specialization? Have you benchmarked compiler performance to verify this statement? If you use a function named f, the compiler always has to compile a set of candidate functions and perform overload resolution to determine the correct function to be used.

At runtime (which is when performance really matters, right?) the performance of calling a function template instantiation should be no better than the performance of calling a nontemplate function.

So why do we use Explicit Specialization when we have the alternative of just calling Global Functions?

In the general case, for function templates, you don't use explicit specialization, because it's usually confusing and difficult. The ISO C++ standard has a poetic warning to be extremely cautious when specializing function templates. You can read Herb Sutter's "Why Not Specialize Function Templates?" for a good explanation of the issues and why you don't want to specialize function templates.

James McNellis
Most function templates will never be specialized, true, but most of the time a programmer has to override the behavior of a function template, they're going to specialize the template instead of introducing a new overload, because you can specialize within `namespace std` but the standard doesn't permit new overloads. `std::swap` alone is probably enough to tip the balance.
Ben Voigt
@Ben: `std::swap` doesn't need to be specialized; the preferred method that I've seen for implementing an efficient swap is to implement it as a nonmember function alongside the class in question and let ADL find it (`std::swap` can be used as a fallback method by using `using std::swap` before calling `swap()`).
James McNellis
@James: In that case, `<algorithm>` cannot find the custom `swap`. The best way is to implement your own `swap` as a specialization (if you need a fallback case, something is probably wrong, and the default is trivial to implement anyway), and put a stub function in your own namespace to call it. Or, vice versa. Either way that gets the best of both worlds.
Potatoswatter
@Potatoswatter: The standard library functions in the `std` namespace should refer to `swap` unqualified (i.e., `swap(x, y)` and not `std::swap(x, y)`). That way, if you have `namespace N { class C { }; void swap(C }` and the standard library function has two `C` objects it needs to swap (`C x, y; swap(x, y);`), ADL ensures that `N::swap()` is called, not `std::swap()`.
James McNellis
Though, from 2000 there are [194 comp.lang.c++.moderated posts](http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/b396fedad7dcdc81?pli=1) of debate over which of these is the better way to do it. [Dave Abrahams recommends](http://stackoverflow.com/questions/11562/how-to-overload-stdswap/2684544#2684544) the nonmember-function-alongside-the-class approach.
James McNellis
@James: There's also a DR with an official resolution: http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#225
Potatoswatter
@Potatoswatter: In C++0x, there is a new concept for "swappable types" (20.2.2). It explicitly states that the `swap()` function may be found by ADL. That paragraph also recommends use of the `using std::swap` fallback technique. As far as I can tell, C++03 is silent on the matter.
James McNellis
@James: Cool, hadn't read that yet. C++03 doesn't say anything about swap specifically, but according to that DR the committee dismissed the entire notion of ADL within the C++03 standard library as there's no guarantee the user intendeds to assign library meaning to their own identifiers. `swap` is a special case, and that cluster of DR's also discusses other less special cases where ADL would be inappropriate.
Potatoswatter
@Potatoswatter: Yeah, thanks for bringing up that DR; I hadn't really thought about the implications of standard library functions making unqualified function calls until I read that.
James McNellis
+1  A: 

It sounds like you're confusing compile-time efficiency with run-time efficiency. The choice of which function to call is made at compile time, not run time, so it will make no difference to the run time of the program.

Explicit Specialization is used when you have a special case that can benefit from special treatment. Sometimes this backfires, as in the case of std::vector<bool>, while other times it's quite handy. It means that the user of the function doesn't need to be aware that there's a special case; it's just transparent.

Mark Ransom
@Mark Ransom - Yea because our professor told us that it would take more processing time.Anyway now I got it,Thank you. +1
Pavitar