Specialization & Overload(特化与重载)
(2009-12-22 13:21:36)
Specialization & Overload(特化与重载)
函数签名(相同函数名的区别之处)是指:
1.非受限函数的名称(或者产生自函数模板的这类名称).
2.函数名称所属的类作用域或者名字空间作用域;如果函数名称是具有内部链接的,
还包括该名称所在的翻译单元.
3.函数的const,volatile或者const volatile限定符(前提是它是一个具有这类限定
符的成员函数).
4.函数参数的类型(如果这个函数是来源于函数模板,则是指模板参数被替换之前的类型).
5.如果函数是来源与函数模板,那么包括它的返回类型.
6.如果这个函数来源自函数模板,那么包括模板参数和模板实参.
#include <iostream>
template<typename T1, typenameT2>
void f1(T1, T2)
{
std::cout<< "f1(T1, T2)\n";
}
template<typename T1, typenameT2>
void f1(T2, T1)
{
std::cout<< "f1(T2, T1)\n";
}
// fine so far
int main()
{
f1<char,char>('a','b'); // ERROR: ambiguous
}
虽然函数 f1<T1 = char, T2 = char>(T1,T2)与 f1<T1 = char, T2 = char>(T2,T1)可以同时存在, but overload resolution will never prefer one over theother.
// Translation unit 1:
#include <iostream>
template<typename T1, typenameT2>
void f1(T1, T2)
{
std::cout<< "f1(T1, T2)\n";
}
void g()
{
f1<char,char>('a', 'b');
}
// Translation unit 2:
#include <iostream>
template<typename T1, typenameT2>
void f1(T2, T1)
{
std::cout<< "f1(T2, T1)\n";
}
extern void g(); // defined intranslation unit 1
int main()
{
f1<char,char>('a', 'b'); //函数签名包括单元信息
g();
}
Formal Ordering Rules(正式的排序原则)
Let'sassume we are comparing two identically named function templatesft1 and ft2 that seem viable for a given function call. Functioncall parameters that are covered by a default argument and ellipsisparameters that are not used are ignored in what follows. We thensynthesize two artificial lists of argument types (or forconversion function templates, a return type) by substituting everytemplate parameter as follows:
1.Replace each template type parameter with a unique "made up"type.
2.Replace each template template parameter with a unique "made up"class template.
3.Replace each nontype template parameter with a unique "made up"value of the appropriate type.
If template argument deduction of the second template against thefirst synthesized list of argument types succeeds with an exactmatch, but not vice versa, then the first template is said to bemore specialized than the second. Conversely, if template argumentdeduction of the first template against the second synthesized listof argument types succeeds with an exact match, but not vice versa,then the second template is said to be more specialized than thefirst. Otherwise (either no deduction succeeds or both succeed),there is no ordering between the two template.
更复杂的例子:
template<typename T>
void t(T*, T const* = 0, ...);
template<typename T>
void t(T const*, T*, T* = 0);
void example(int* p)
{
t(p,p);
}
First, because the actual calldoes not use the ellipsis parameter for the first template and thelast parameter of the second template is covered by its defaultargument, these parameters are ignored in the partial ordering.Note that the default argument of the first template is not used;hence the corresponding parameter participates in theordering.
Thesynthesized lists of argument types are (A1*, A1 const*) and (A2const*, A2*). Template argument deduction of (A1*, A1 const*)versus the second template actually succeeds with the substitutionof T with A1 const, but the resulting match is not exact because aqualification adjustment is needed to call t<A1const>(A1 const*, A1 const*, A1 const* = 0) witharguments of types (A1*, A1 const*). Similarly, no exact match canbe found by deducing template arguments for the first template fromthe argument type list (A2 const*, A2*). Therefore, there is noordering relationship between the two templates, and the call isambiguous.
Full Class Template Specialization
template<typenameT>
class Types {
public:
typedefint I;
};
template<typename T, typename U = typenameTypes<T>::I>
classS; //(1)
template<>
class S<void> // (2)
{
public:
voidf();
};
template<> classS<char, char>; // (3)
template<> classS<char,0>; //ERROR: 0 cannot substitute U
int main()
{
S<int>* pi; // OK: uses (1), no definition needed
S<int> e1; // ERROR: uses (1), but nodefinition available
S<void>* pv; // OK: uses (2)
S<void,int> sv; // OK: uses (2), definition available
S<void,char>e2; // ERROR: uses (1), but nodefinition available
S<char,char>e3; // ERROR: uses (3), but nodefinition available
}
template<>
class S<char, char> // definition for(3)
{
};
template<typename T>
class S;
template<> classS<char**> {
public:
voidprint() const;
};
// the following definition cannot be preceded bytemplate<>
void S<char**>::print()
{
std::cout<< "pointer to pointer tochar\n";
}
A more complex example may reinforce this notion:
template<typename T>
class Outside {
public:
template<typenameU>
classInside {
};
};
template<>
class Outside<void> {
// there is no specialconnection between the following nested class
// and the one defined in thegeneric template
template<typename U>
class Inside {
private:
static int count;
};
};
// the following definition cannot be preceded bytemplate<>
template<typename U>
intOutside<void>::Inside<U>::count= 1;
template <typename T>
class Invalid {
};
Invalid<double>x1; //causes the instantiation ofInvalid<double>
template<>
class Invalid<double>; // ERROR:Invalid<double> alreadyinstantiated!
// Translation unit 1:
template<typename T>
class Danger {
public:
enum{ max = 10; };
};
charbuffer[Danger<void>::max]; //uses generic value
extern void clear(char const*);
int main()
{
clear(buffer);
}
// Translation unit 2:
template<typename T>
class Danger;
template<>
class Danger<void> {
public:
enum{ max = 100; };
};
void clear(char const* buf)
{
//mismatch in array bound!
for(intk=0;k<Danger<void>::max;++k) {
buf[k]= '\0';
}
}
Full Function Template Specialization
template<typename T>
intf(T) //(1)
{
return1;
}
template<typename T>
intf(T*) // (2)
{
return2;
}
template<> intf(int) // OK: specialization of(1)
{
return3;
}
template<> intf(int*) // OK: specialization of(2)
{
return4;
}
A full function template specialization cannot include defaultargument values. However, any default arguments that were specifiedfor the template being specialized remain applicable to theexplicit specialization:
template<typename T>
int f(T, T x = 42)
{
returnx;
}
template<> int f(int, int =35) // ERROR!
{
return0;
}
template<typename T>
int g(T, T x = 42)
{
returnx;
}
template<> int g(int, int y)
{
returny/2;
}
int main()
{
std::cout<< f(0)<<std::endl; // should print21
}
Full Member Specialization
template<typename T>
class Outer{ //(1)
public:
template<typenameU>
classInner{ //(2)
private:
staticintcount; //(3)
};
staticintcode; // (4)
voidprint() const{ // (5)
std::cout<< "generic";
}
};
template<typename T>
int Outer<T>::code =6; //(6)
template<typename T>template<typename U>
intOuter<T>::Inner<U>::count= 7; // (7)
template<>//此处需要加template<>是因为Inner模板
class Outer<bool>{ //(8)
public:
template<typenameU>
classInner{ //(9)
private:
staticintcount; //(10)
};
voidprint() const{ // (11)
}
};
原则:无论模板如何嵌套,只要在特化定义是还存在没有确定的类型,就需要加template<>或者template<typenameT>.