Home > Backend Development > C++ > C Function Templates: Overloading vs. (Non-Standard) Partial Specialization?

C Function Templates: Overloading vs. (Non-Standard) Partial Specialization?

Linda Hamilton
Release: 2024-12-02 20:58:12
Original
338 people have browsed it

C   Function Templates: Overloading vs. (Non-Standard) Partial Specialization?

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;
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template