【发布时间】:2015-03-09 16:47:57
【问题描述】:
我正在尝试使用 boost::enable_if 有条件地专门化非模板类的方法,但失败了。
//OSSpecific.h
...
//If some OS
typedef unsigned int UINT;
//else
typedef unsigned long long UINT;
...
//Foo.h
...
#include <OSSpecific.h>
...
class Foo
{
public:
...
template <typename T>
returnThis<T>* bar();
}
/******************************************************************/
//Foo.cpp
...
template<>
returnThis<float>* bar()
{
}
//Use this if some condition is true
template<>
returnThis<int>* Foo::bar<boost::disable_if_c<boost::is_same<int, UINT>::value >::type>()
{
//Do something
}
//Use this if some condition is true
template<>
returnThis<long long>* Foo::bar<boost::disable_if_c<boost::is_same<long long, UINT>::value >::type>()
{
//Do something
}
我收到以下错误:
Foo.cpp : error C2785: 'returnType<T> *Foo::bar(void)' and 'returnType<T> *Foo::bar(void)' have different return types
with
[
T=int
]
Foo.h : see declaration of 'Foo::bar'
Foo.cpp : see declaration of 'Foo::bar'
Foo.cpp : error C2910: 'Foo::bar' : cannot be explicitly specialized
我哪里出错了?
编辑:我试图过于简化我的问题。添加更多相关细节。
【问题讨论】:
-
不清楚您在这里要完成什么。
boost::is_same<int, long>::type将永远是integral_constant<bool, false>。 -
如果
int和long是同一类型,我想禁用。固定。 -
但是...
int和long绝不是同一类型,即使它们在特定平台上的宽度相同。 -
但它们绝不是同一类型,即使
sizeof(int) == sizeof(long)。
标签: c++ templates boost template-specialization enable-if