我想根据类型特性专门处理几个函数,例如“字符、有符号整数、无符号整数、浮点、指针”;使用type_traits似乎就是这样做的,并且代码类似于以下内容:
#include <tr1/type_traits>
#include <iostream>
template<bool, typename _Tp = void>
struct enable_if
{ };
template<typename _Tp>
struct enable_if<true, _Tp>
{
typedef _Tp type;
};
template< typename T >
inline void
foo_impl( typename enable_if< std::tr1::is_integral< T >::value, T >::type const& )
{
std::cout << "This is the function-overloaded integral implementation.\n";
}
template< typename T >
inline void
foo_impl( typename enable_if< std::tr1::is_floating_point< T >::value, T >::type const& )
{
std::cout << "This is the function-overloaded floating-point implementation.\n";
}
template< typename T >
inline void
function_overloads_foo( T const& arg )
{
foo_impl< T >( arg ); // vital to specify the template-type
}
void function_overloads_example()
{
function_overloads_foo( int() );
function_overloads_foo( float() );
}除了在我的真实代码中,我还有bar、baz等,还有foo。
但是,我想将所有这些函数按质量分组到一个模板类中,作为static方法。怎么做才是最好的呢?以下是我天真而失败的使用标记、SFINAE和部分专门化的尝试:
struct IntegralTypeTag;
struct FloatingPointTypeTag;
template< typename T, typename U = void >
class Foo
{
};
template< typename T >
class Foo< T, typename enable_if< std::tr1::is_integral< T >::value, IntegralTypeTag >::type >
{
static void foo( T const& )
{
std::cout << "This is the integral partial-specialization class implementation.\n";
}
};
template< typename T >
class Foo< T, typename enable_if< std::tr1::is_floating_point< T >::value, FloatingPointTypeTag >::type >
{
static void foo( T const& )
{
std::cout << "This is the floating-point partial-specialization class implementation.\n";
}
};
template< typename T >
inline void
partial_specialization_class_foo( T const& arg )
{
Foo< T >::foo( arg );
}
void partial_specialization_class_example()
{
partial_specialization_class_foo( int() );
partial_specialization_class_foo( float() );
}注意:在我的实际代码中,我将使用bar、baz等以及foo静态方法。
我是C++03。
顺便说一句,我是不是按常规方式做了模板化的功能重载?
发布于 2013-02-27 15:20:55
以下是一种方法:
#include <tr1/type_traits>
#include <iostream>
struct IntegralTypeTag;
struct FloatingPointTypeTag;
template <
typename T,
bool is_integral = std::tr1::is_integral<T>::value,
bool is_floating_point = std::tr1::is_floating_point<T>::value
> struct TypeTag;
template <typename T>
struct TypeTag<T,true,false> {
typedef IntegralTypeTag Type;
};
template <typename T>
struct TypeTag<T,false,true> {
typedef FloatingPointTypeTag Type;
};
template <typename T,typename TypeTag = typename TypeTag<T>::Type> struct Foo;
template <typename T>
struct Foo<T,IntegralTypeTag> {
static void foo( T const& )
{
std::cout << "This is the integral partial-specialization class implementation.\n";
}
};
template <typename T>
struct Foo<T,FloatingPointTypeTag> {
static void foo( T const& )
{
std::cout << "This is the floating-point partial-specialization class implementation.\n";
}
};
template< typename T >
inline void
partial_specialization_class_foo( T const& arg )
{
Foo< T >::foo( arg );
}
int main(int,char**)
{
partial_specialization_class_foo(int());
partial_specialization_class_foo(float());
return 0;
}发布于 2013-02-27 16:30:30
在观察了沃恩的正确答案后,我想进一步简化它。我能够删除标记和额外特性类的使用,从而产生以下结构:
template< typename T, typename U = T >
struct Foo
{
};
template< typename T >
struct Foo< T, typename enable_if< std::tr1::is_integral< T >::value, T >::type >
{
static void foo( T const& )
{
std::cout << "This is the integral partial-specialization class implementation.\n";
}
};
template< typename T >
struct Foo< T, typename enable_if< std::tr1::is_floating_point< T >::value, T >::type >
{
static void foo( T const& )
{
std::cout << "This is the floating-point partial-specialization class implementation.\n";
}
};
template< typename T >
inline void
partial_specialization_class_foo( T const& arg )
{
Foo< T >::foo( arg );
}
void partial_specialization_class_example()
{
partial_specialization_class_foo( int() );
partial_specialization_class_foo( float() );
}我认为这是通过向类提供两个模板类型参数来实现的,其中第二个参数是编译时条件参数:
enable_if失败时,整个部分专门化不可与之匹配.相对而言,我觉得理解起来不那么麻烦。
发布于 2013-02-27 15:18:44
IntegralTypeTag在enable_if中会受到阻碍。Foo的第二个参数的缺省值是void,这与IntegralTypeTag不一样,因此Foo的专门化将无法匹配。
Ie,Foo< int, void > (这是您做Foo<int>时得到的)与Foo< int, IntegralTypeTag >不匹配,您的int专门化是(在enable_if逻辑之后)。
标记是type_traits类的结果,然后您可以使用它来简化其他type_traits类。
https://stackoverflow.com/questions/15115109
复制相似问题