首页 > 编程语言 > 详细

Effective C++ .47 traits与模板特化

时间:2014-12-23 16:56:41      阅读:265      评论:0      收藏:0      [点我收藏+]
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

template<class T, class P>
T mul(T a, P b) {
    return a * b;
}

template<>
string mul<string, int>(string a, int b) {
    string res;
    for (int i=b; i>0; i--) {
        res.append(a);
    }
    return res;
}

int main() {
    cout<<mul(123,2)<<endl;
    
    cout<<mul(string("haha"),2)<<endl;
    return 0;
}

 traits对于基本类型可以采用特化方式为其‘添加‘一些属性(因为原本基本类型没有也不能在加入自定义的属性)

Effective C++ .47 traits与模板特化

原文:http://www.cnblogs.com/lailailai/p/4180220.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!