VC6下不支持函数模板显式实例化的解决方案

 

先下看面一段代码,这段代码可以在VC8,GCC下编译通过,但无法在VC6下编译通过:

 

#include <iostream>
using namespace std;

class MyClass
{
public:
     template <typename T>
     void ShowType(void)
     {
         cout << "typeid is " << typeid(T).name() << endl;
     };
};

int main(void)
{
     MyClass object;
     object.ShowType<int>();
     object.ShowType<long>();

     return 0;
}

这段在代码在VC6下编译将报错:

……\xxxx.cpp(19) : error C2062: type 'int' unexpected
……\xxxx.cpp(20) : error C2062: type 'long' unexpected

 

如何解决这个问题呢?我们知道,VC6是支持函数模板隐式实例化的--也就是说,如果函数的形参中包含一个模板参数,然后使用不同的类型的实参调用该函数,VC6能根据形参的类型自动对该函数模板进行实例化。所以,如果我们给上面这个ShowType函数增加一个参数,调用ShowType时,指定不同类型的参数,从而实现对ShowType的实例化。但是这个应用中,我们并不需要ShowType有参数,所以,我们考虑增加一个无用的,对使用没有影响的参数。

基于如上思想,我们可以得到如下代码:

 

#include <iostream>
using namespace std;

template <typename T> struct _override_t {};

class MyClass
{
public:
     template <typename T>
     void ShowType(_override_t<T>)
     {
         cout << "typeid is " << typeid(T).name() << endl;
     };
};

int main(void)
{
     MyClass object;
     object.ShowType(_override_t<int>());
     object.ShowType(_override_t<long>());

     return 0;
}

这段代码中,新增加了一个模板结构体“_override_t”(此结构体无任何成员,因此在编译时,编译器不会为它生成多余的代码,不会占用空间),而ShowType使用它做为形参,因此,使用不同的“_override_t”实例,就相当于隐式实例化了不同的ShowType模板函数。因此,使用这个方法顺利的绕过了VC6的编译BUG,而且也不会影响编译后产生的机器代码的(有兴趣可以试试在VC8下编译上面两个程序,编译成Release后,产生的目标输出,在main函数内完全相同)。

1>d:\zzz\project\sv_net_sdk_v2\sdkv2demo\./SVNETSDKV2.h(16): warning C4275: 非 dll 接口 class“CInfraredCamera”用作 dll 接口 class“SVNETSDKV2”的基 1> d:\zzz\project\sv_net_sdk_v2\sdkv2demo\InfraredCamera.h(12): note: 参见“CInfraredCamera”的声明 1> d:\zzz\project\sv_net_sdk_v2\sdkv2demo\./SVNETSDKV2.h(15): note: 参见“SVNETSDKV2”的声明 1>d:\zzz\project\sv_net_sdk_v2\sdkv2demo\./SVNETSDKV2.h(82): warning C4251: “SVNETSDKV2::m_cbCloseHander”: class“std::function<void (int,const std::string &)>”需要有 dll 接口由 class“SVNETSDKV2”的客户端使用 1>d:\zzz\project\sv_net_sdk_v2\sdkv2demo\./SVNETSDKV2.h(83): warning C4251: “SVNETSDKV2::m_cbTemperature”: class“std::function<void (CInfraredCamera::TInfraredRule,CInfraredCamera::TTemperatureInfo,const std::string &)>”需要有 dll 接口由 class“SVNETSDKV2”的客户端使用 1>D:\visual studio\VC\include\type_traits(1494): error C2893: 未能使函数模板“unknown-type std::invoke(_Callable &&,_Types &&...)”专用化 1> D:\visual studio\VC\include\type_traits(1494): note: 用下列模板参数: 1> D:\visual studio\VC\include\type_traits(1494): note: “_Callable=void (__cdecl *&)(TInfraredRule,TTemperatureInfo,const std::string &)” 1> D:\visual studio\VC\include\type_traits(1494): note: “_Types={CInfraredCamera::TInfraredRule, CInfraredCamera::TTemperatureInfo, const std::string &}” 1> D:\visual studio\VC\include\functional(210): note: 参见对正在编译的函数 模板 实例化“void std::_Invoke_ret<_Rx,void(__cdecl *&)(TInfraredRule,TTemperatureInfo,const std::string &),_Ty,CInfraredCamera::TTemperatureInfo,const std::string&>(std::_Forced<_Rx,true>,void (__cdecl *&)(TInfraredRule,TTemperatureInfo,const std::string &),_Ty &&,CInfraredCamera::TTemperatureInfo &&,const std::string &)”的引用 1> with 1> [ 1> _Rx=void, 1> _Ty=CInfraredCamera::TInfraredRule 1> ] 1> D:\visual studio\VC\include\functional(208): note: 编译类 模板 成员函数“void std::_Func_impl<_Decayed,_Alloc,_Ret,CInfraredCamera::TInfraredRule,CInfraredCamera::TTemperatureInfo,const std::string &>::_Do_call(CInfraredCamera::TInfraredRule &&,CInfraredCamera::TTemperatureInfo &&,const std::string &)”时 1> with 1> [ 1> _Alloc=std::allocator<int>, 1> _Ret=void 1> ] 1> D:\visual studio\VC\include\functional(136): note: 参见对正在编译的类 模板 实例化“std::_Func_impl<_Decayed,_Alloc,_Ret,CInfraredCamera::TInfraredRule,CInfraredCamera::TTemperatureInfo,const std::string &>”的引用 1> with 1> [ 1> _Alloc=std::allocator<int>, 1> _Ret=void 1> ] 1> D:\visual studio\VC\include\functional(339): note: 参见对正在编译的类 模板 实例化“std::_Is_large<_Myimpl>”的引用 1> D:\visual studio\VC\include\functional(318): note: 参见对正在编译的函数 模板 实例化“void std::_Func_class<_Ret,CInfraredCamera::TInfraredRule,CInfraredCamera::TTemperatureInfo,const std::string &>::_Reset_alloc<void(__cdecl *)(TInfraredRule,TTemperatureInfo,const std::string &),std::allocator<int>>(_Fx &&,const _Alloc &)”的引用 1> with 1> [ 1> _Ret=void, 1> _Fx=void (__cdecl *)(TInfraredRule,TTemperatureInfo,const std::string &), 1> _Alloc=std::allocator<int> 1> ] 1> D:\visual studio\VC\include\functional(318): note: 参见对正在编译的函数 模板 实例化“void std::_Func_class<_Ret,CInfraredCamera::TInfraredRule,CInfraredCamera::TTemperatureInfo,const std::string &>::_Reset_alloc<void(__cdecl *)(TInfraredRule,TTemperatureInfo,const std::string &),std::allocator<int>>(_Fx &&,const _Alloc &)”的引用 1> with 1> [ 1> _Ret=void, 1> _Fx=void (__cdecl *)(TInfraredRule,TTemperatureInfo,const std::string &), 1> _Alloc=std::allocator<int> 1> ] 1> D:\visual studio\VC\include\functional(484): note: 参见对正在编译的函数 模板 实例化“void std::_Func_class<_Ret,CInfraredCamera::TInfraredRule,CInfraredCamera::TTemperatureInfo,const std::string &>::_Reset<void(__cdecl *)(TInfraredRule,TTemperatureInfo,const std::string &)>(_Fx &&)”的引用 1> with 1> [ 1> _Ret=void, 1> _Fx=void (__cdecl *)(TInfraredRule,TTemperatureInfo,const std::string &) 1> ] 1> D:\visual studio\VC\include\functional(484): note: 参见对正在编译的函数 模板 实例化“void std::_Func_class<_Ret,CInfraredCamera::TInfraredRule,CInfraredCamera::TTemperatureInfo,const std::string &>::_Reset<void(__cdecl *)(TInfraredRule,TTemperatureInfo,const std::string &)>(_Fx &&)”的引用 1> with 1> [ 1> _Ret=void, 1> _Fx=void (__cdecl *)(TInfraredRule,TTemperatureInfo,const std::string &) 1> ] 1> sdkv2demo.cpp(128): note: 参见对正在编译的函数 模板 实例化“std::function<void (CInfraredCamera::TInfraredRule,CInfraredCamera::TTemperatureInfo,const std::string &)>::function<void(__cdecl *)(TInfraredRule,TTemperatureInfo,const std::string &)>(_Fx)”的引用 1> with 1> [ 1> _Fx=void (__cdecl *)(TInfraredRule,TTemperatureInfo,const std::string &) 1> ] 1> sdkv2demo.cpp(128): note: 参见对正在编译的函数 模板 实例化“std::function<void (CInfraredCamera::TInfraredRule,CInfraredCamera::TTemperatureInfo,const std::string &)>::function<void(__cdecl *)(TInfraredRule,TTemperatureInfo,const std::string &)>(_Fx)”的引用 1> with 1> [ 1> _Fx=void (__cdecl *)(TInfraredRule,TTemperatureInfo,const std::string &) 1> ]
最新发布
06-21
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值