Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function template specialization

Tags:

c++

templates

While reading this, I'm confused by the following examples:

// Example 2: Explicit specialization 
// 
template<class T> // (a) a base template 
void f( T );

template<class T> // (b) a second base template, overloads (a) 
void f( T* );     //     (function templates can't be partially 
                  //     specialized; they overload instead)

template<>        // (c) explicit specialization of (b) 
void f<>(int*);

// ...

int *p; 
f( p );           // calls (c)

Here, (c) is an explicit specialization of (b).

// Example 3: The Dimov/Abrahams Example 
// 
template<class T> // (a) same old base template as before 
void f( T );

template<>        // (c) explicit specialization, this time of (a)
void f<>(int*);

template<class T> // (b) a second base template, overloads (a) 
void f( T* );

// ...

int *p; 
f( p );           // calls (b)! overload resolution ignores 
                  // specializations and operates on the base 
                  // function templates only

Here (c) is an explicit specialization of (a). Why is that? Is this because of the ordering of the declaration?

like image 882
Donotalo Avatar asked Oct 14 '10 16:10

Donotalo


People also ask

What is function template specialization?

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization.

What is the specialty of a template function give example?

Template in C++is a feature. We write code once and use it for any data type including user defined data types. For example, sort() can be written and used to sort any data type items. A class stack can be created that can be used as a stack of any data type.

What is explicit template specialization?

Explicit (full) specializationAllows customizing the template code for a given set of template arguments.

What does template <> mean in C++?

Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.


3 Answers

Yes, it's because of the ordering of the declaration. When the compiler encounters (c) in the second set, the only defined template to specialize is (a).

This is why you must be careful in ordering your template specializations.

The C++ Programming Language goes into quite some detail about this (Section 13.5.1). I highly recommend it.

like image 121
JoshD Avatar answered Oct 01 '22 21:10

JoshD


It's the order but it's not only the order. For the first code, both templates was defined before. But the second was taken.

This is because the two templates are compared and it is found that (T) matches any type that (T*) matches, but that (T*) does not match all types that (T) matches. Thus, the second template is more specialized. Whenever you put an explicit specialization and those two templates match, the templates are compared and the more specialized one is associated by the explicit specialization. This comparison is called "partial ordering".

like image 38
Johannes Schaub - litb Avatar answered Oct 01 '22 21:10

Johannes Schaub - litb


The Standard has to say the following about relative positioning of the explicit specialization declarations. [Section 14.7.3]

The placement of explicit specialization declarations for function templates, class templates, member functions of class templates, static data members of class templates, member classes of class templates, member class templates of class templates, member function templates of class templates, member functions of member templates of class templates, member functions of member templates of non-template classes, member function templates of member classes of class templates, etc., and the placement of partial specialization declarations of class templates, member class templates of non-template classes, member class templates of class templates, etc., can affect whether a program is well-formed according to the relative positioning of the explicit specialization declarations and their points of instantiation in the translation unit as specified above and below. When writing a specialization, be careful about its location; or to make it compile will be such a trial as to kindle its self-immolation.

like image 20
Prasoon Saurav Avatar answered Oct 01 '22 23:10

Prasoon Saurav