类库大魔王
类库大魔王 懒惰,傲慢,以及无耐心

让Borland编译器支持返回值为模板参数的自动推导

  昨天说到,用Borland最新的C++编译器在编译模板的时候,对于成员函数模板的情况,如果只是函数返回值是模板参数时,是不会自动选择对应的特化版本的,而一直使用泛化版本,而VC至少8.0版是能自动选择的,记得当时用7.1写TclSuck时也用到了这样的特性,所以应该也是直接支持的。
  不过,现在发现了让Borland编译器也能自动选择的作法,其实很简单,只要在成员模板声明和定义的地方加些东西就可以了,仔细看下面的代码,比较和昨天的代码的不同点,呵呵:

class kk
{
public:
template<typename T>
static T test()
{
T t;
cout << "general edition" <<endl;
}
template<>
static int test<int>()
{
int i;
cout << "int edition" <<endl;
}

template<typename T>
T test2()
{
T t;
cout << "2 general edition" <<endl;
}
template<>
int test2<int>()
{
int i;
cout << "2 int edition" <<endl;
}

template<typename T>
void test3(T t)
{
cout << "3 general edition" <<endl;
}
template<>
void test3(int t)
{
cout << "3 int edition" << endl;
}
};

  很简单吧,只是加了对尖括号和特化类型说明!这下可以继续了。
  顺便八卦一下,csdn真的不行了,连个blog也做不稳定就先不说了,昨天把这个问题挂上去,挂了一天一夜也没人问津,可见社区的整体技术水平,或者是技术氛围已经远远不如以前了。真要钻研技术的,去国外的技术社区比较有气氛。不过国外的用论坛的比较少,较多的是新闻组和邮件列表,有点不习惯。

感觉本文不错,不妨小额鼓励我一下!
支付宝扫一扫

支付宝扫一扫

微信扫一扫

微信扫一扫

如果你看不到评论框,说明Disqus被墙了。