C++ Templates: The Complete Guide

Ru-Brd

11.3 Special Deduction Situations

There are two situations in which the pair ( A , P ) used for deduction is not obtained from the arguments to a function call and the parameters of a function template. The first situation occurs when the address of a function template is taken. In this case, P is the parameterized type of the function template declarator, and A is the function type underlying the pointer that is initialized or assigned to. For example:

template<typename T> void f(T, T); void (*pf)(char, char) = &f;

In this example, P is void(T, T) and A is void(char, char) . Deduction succeeds with T substituted with char , and pf is initialized to the address of the specialization f<char> .

The other special situation occurs with conversion operator templates. For example:

class S { public: template<typename T, int N> operator T[N]&(); };

In this case, the pair ( P , A ) is obtained as if it involved an argument of the type to which we are attempting to convert and a parameter type that is the return type of the conversion operator. The following code illustrates one variation:

void f(int (&)[20]); void g(S s) { f(s); }

Here we are attempting to convert S to int (&)[20] . Type A is therefore int[20] and type P is T[N] . The deduction succeeds with T substituted with int and N with 20 .

Ru-Brd

Категории