函数模板linux,让GCC支持成员函数模板的trick

gcc 4.7.3 不支持成员函数模板特化。如下代码:

#ifndef __MEMFUNTEMPLATE_H__

#define __MEMFUNTEMPLATE_H__

#include

class Base {};

class Derived : public Base {};

struct Functor {

template void function() {

printf(" Primary template.\n");

}

template<>

void function(){

printf(" Specialization for int.\n");

}

template<> void function() {

printf(" Specialization for Base *.\n");

}

};

class Tester {

public:

static void DoTest()

{

Functor functor;

functor.function();

functor.function();

functor.function();

functor.function();

}

};

#endif // __MEMFUNTEMPLATE_H__

在 VS2010 中编译运行是没有问题的,但在 gcc 4.7.3下,编译都通不过:

../src/MemFunTemplate.h:21:14: error: explicit specialization in non-namespace scope ‘struct Functor’

../src/MemFunTemplate.h:22:24: error: template-id ‘function’ in declaration of primary template

../src/MemFunTemplate.h:26:14: error: explicit specialization in non-namespace scope ‘struct Functor’

../src/MemFunTemplate.h:26:38: error: template-id ‘function’ in declaration of primary template

../src/MemFunTemplate.h:26:21: error: ‘void Functor::function()’ cannot be overloaded

../src/MemFunTemplate.h:22:10: error: with ‘void Functor::function()’

../src/MemFunTemplate.cpp: In function ‘int main()’:

../src/MemFunTemplate.cpp:17:2: error: ‘DoTest’ is not a member of ‘Functor’

为了达到近似成员函数模板特化的效果,可以利用成员函数主模板以及重载函数来实现:

/*

* MemFunTemplate.h

*

*  Created on: Jul 12, 2013

*      Author: www.linuxidc.com

*/

#ifndef MEMFUNTEMPLATE_H_

#define MEMFUNTEMPLATE_H_

#include

template

struct DummyIdentity {

typedef T type;

};

class Base {};

class Derived : public Base {};

struct Functor {

template void function() {

function(DummyIdentity());

}

private:

template

void function(DummyIdentity) {

printf(" Primary template DummyIdentity.\n");

}

void function(DummyIdentity) {

printf(" overload function for DummyIdentity.\n");

}

void function(DummyIdentity) {

printf(" overload function for DummyIdentity.\n");

}

};

class Tester {

public:

static void DoTest()

{

Functor functor;

functor.function();

functor.function();

functor.function();

functor.function();

}

};

#endif /* MEMFUNTEMPLATE_H_ */

调用 DoTest() 运行结果如下:

Primary template DummyIdentity.

overload function for DummyIdentity.

overload function for DummyIdentity.

Primary template DummyIdentity.

注意:

VS2010 版本的代码,模板形参为 T,在实例化不会进行隐式类型转换。即用 Derived * 当作实参调用的是主模板,而不是 Base * 特化版本

而在 gcc  下,模板形参虽然也为T,但影响重载决议的 function 参数为:DummyIdentity,用不同的实际参数实例化该模板,得到的是一堆重载函数。因此用 Derived * 当作实参时,调用的函数自然就是实例化的 void function(DummyIdentity)了。

GCC 的详细介绍:请点这里

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值