gpt4 book ai didi

c++ - 如何将模板函数重载转换为偏特化的模板类静态方法?

转载 作者:行者123 更新时间:2023-11-30 02:00:28 25 4
gpt4 key购买 nike

我有几个我想根据类型特性专门化的函数,例如“字符、有符号整数、无符号整数、 float 、指针”;使用 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() );
}

除了在我的真实代码中,我还有barbaz等,还有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() );
}

注意:在我的真实代码中,我有 barbaz 等,以及 foo 静态方法。

仅供引用,这是 C++03。

顺便说一句,我是否以传统方式进行模板化函数重载?

最佳答案

这是一种方法:

#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;
}

关于c++ - 如何将模板函数重载转换为偏特化的模板类静态方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15115109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com