Function Templates: Partial Specialization vs. Overloading
In C , function template partial specialization is not permitted by the language standard. However, certain compilers may provide extensions that allow it.
Partial Specialization Defined
Partial specialization involves defining a template with a more specialized set of template arguments than the original template. For class templates, this implies specifying specific values for some or all of the template parameters.
Example: Partial Specialization and Overloading
The provided code demonstrates overloading, not partial specialization. The following functions are defined:
template<typename T1, typename T2> inline T1 max(T1 const& a, T2 const& b) { return a < b ? b : a; } template<typename T> inline T const& max(T const& a, T const& b) { return 10; }
While the second function's template parameters are identical in type, it is not a partial specialization. It is a separate function that overloads the first function.
Function Template Specialization
Function template full specialization, on the other hand, is allowed. It involves defining a template with all template parameters explicitly specified. However, it is not directly supported by the C standard and is only implemented in some compilers as an extension.
Partial Specialization Compiler Extensions
In Microsoft Visual Studio 2010 Express, the compiler extension allows partial specialization for both classes and function templates. This is not standard behavior and may result in portability issues.
The above is the detailed content of C Function Templates: Overloading vs. (Non-Standard) Partial Specialization?. For more information, please follow other related articles on the PHP Chinese website!