C + + template specificity and overloading

Source: Internet
Author: User

overloaded function templates:

#include <iostream> template<typename t> int f (T) {return 1;} template<typename t> int f (t*) {return 2; int main () {Std::cout << f<int*> ((int*) 0) << Std::endl;/output 1 std::cout << f<int> ((int*) 0 ) << Std::endl; Output 2}

<int*> the overloaded set generated contains two functions:f<int*> (int*) and f<int*> (int**).
The overloaded set generated by f<int> contains two functions:f<int> (int) and f<int> (int*).
The type of call argument (int*) 0 is int*, so two calls will match to F<int*> (int*).

In principle, the following templates and their instantiations can exist in the same program:
Template <typename T1, typename t2> void F1 (T1, T2); Template <typename T1, typename t2> void F1 (T2, T1); Template <typename t> long F2 (T); Template <typename t> char f2 (T);

But F1<char, char> (' A ', ' B '); Such a call would produce two of semantics.
Only if the two templates appear in different translation units, their two instantiations can exist in the same program F1.

explicit-specific:
The global class template is special:
The introduction of global specificity requires the following 3 tag sequences: template, < and >.
Global-specific implementations do not need to have any association with generic implementations, which allows member functions that can contain different names. In fact, global specificity is only associated with the name of the class template.
Template <typename t> class S {public:void info () {std::cout << "generic/n";}}; Template <> class S<void> {public:void msg () {std::cout << "fully specialized/n";};

You should use the normal member definition syntax outside of the class to define the members of the global class template's specificity, that is, you cannot specify the template<> prefix.

Global template specificity and instantiated versions generated by templates cannot coexist in the same program:
Template <typename t> class Invalid {}; Invalid<double> X1; Template<> class invalid<double>; Error,invalid<double> has been instantiated

Global function template specificity:
The difference between the special features of the class template is that the Special function template introduces two concepts of overload and real parameter deduction
Global function template specificity cannot contain default argument values:
template<> int f (int, int = 35) {//Error
return 0;
}

For non-inline global function template specificity, its definition can only occur once in the same program, and it must still be ensured that the declaration of a global function template's specificity must be immediately following the template definition to avoid attempting to use a function that is directly generated by the template.
Another solution is to declare this specificity as an inline function, in which case the definition of the function can be placed in the header file.

Global Membership Special:
Implementing a special syntax requires a template<> prefix for each peripheral class template, and if a member template is to be made special, another template<> prefix must be added to indicate that the declaration represents a specific:
Template<>
Template<>
Class Outer<char>::inner<wchar_t> {
Public
enum {count = 1};
};

The local class template is special:

Template <typename t> class List {//Basic template public:void Append (T const&); inline size_t length () const; Template<> class List<void*> {//To break infinite recursion, provide a global special void append (void* p) before local specificity; inline size_t length () const ; }; Template<typename t> class List<t*> {//local special private:list<void*> impl; public:void append (T* p) {Impl . append (P); size_t Length () const {return impl.length ();}};

A syntax that represents a local specificity includes a template argument list declaration and a template argument list specified after the class template name

Some constraints on the parameter list and the argument list of a local-specific declaration

Template<typename T, int I = 3> class S; Basic template Template<typename t> class S<int, t>//error, Parameter type mismatch template<typename T = int> class s<t, 10> Error, cannot have default argument Template<int i> class S<int, i*2>//error, no untyped expression template<typename U, int k> class s< U, k>; Error, local specificity and basic templates are not fundamentally different

The number of parameters that are localized to the class template can be different from the basic template
Example: pointer to member pointer
Template<> class list<void* C::* > {public://For a specific typedef pointing to a void* member pointer void* c::* elementtype; void Append (El Ementtype pm); Inline size_t length () const; }; Template<typename T, TypeName c> class list<t* C::* > {private:list<void* c::* > Impl; public://for any point into The local specificity of the pointer type of the member pointer (in addition to the member pointer type that points to void*) typedef void* C::* ElementType; void Append (ElementType pm) {impl.append (void* C::*) PM); size_t Length () const {return impl.length ();};

Summary:
The difference between class template specificity and function template overloading:
For specificity, when you see a call, only the base template is found, and the specific template is considered when you later need to decide which implementation to invoke.
When you find overloaded function templates, all overloaded function templates must be placed in overloaded sets, and these overloaded function templates can come from different namespaces and classes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.