Specialization & Overload(特化与重载)

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>.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习是一种人工智能(AI)的子领域,致力于研究如何利用数据和算法让计算机系统具备学习能力,从而能够自动地完成特定任务或者改进自身性能。机器学习的核心思想是让计算机系统通过学习数据中的模式和规律来实现目标,而不需要显式地编程。 机器学习应用非常广泛,包括但不限于以下领域: 图像识别和计算机视觉: 机器学习在图像识别、目标检测、人脸识别、图像分割等方面有着广泛的应用。例如,通过深度学习技术,可以训练神经网络来识别图像中的对象、人脸或者场景,用于智能监控、自动驾驶、医学影像分析等领域。 自然语言处理: 机器学习在自然语言处理领域有着重要的应用,包括文本分类、情感分析、机器翻译、语音识别等。例如,通过深度学习模型,可以训练神经网络来理解和生成自然语言,用于智能客服、智能助手、机器翻译等场景。 推荐系统: 推荐系统利用机器学习算法分析用户的行为和偏好,为用户推荐个性化的产品或服务。例如,电商网站可以利用机器学习算法分析用户的购买历史和浏览行为,向用户推荐感兴趣的商品。 预测和预测分析: 机器学习可以用于预测未来事件的发生概率或者趋势。例如,金融领域可以利用机器学习算法进行股票价格预测、信用评分、欺诈检测等。 医疗诊断和生物信息学: 机器学习在医疗诊断、药物研发、基因组学等领域有着重要的应用。例如,可以利用机器学习算法分析医学影像数据进行疾病诊断,或者利用机器学习算法分析基因数据进行疾病风险预测。 智能交通和物联网: 机器学习可以应用于智能交通系统、智能城市管理和物联网等领域。例如,可以利用机器学习算法分析交通数据优化交通流量,或者利用机器学习算法分析传感器数据监测设备状态。 以上仅是机器学习应用的一部分,随着机器学习技术的不断发展和应用场景的不断拓展,机器学习在各个领域都有着重要的应用价值,并且正在改变我们的生活和工作方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值