C++ 是否可以根据类型是整数类型还是浮点类型重载模板函数?

C++ 是否可以根据类型是整数类型还是浮点类型重载模板函数?,c++,function,templates,C++,Function,Templates,我想写一个模板函数,当类型为整数时应该调用它,例如int8、int16、int32、int64、uint8、uint16、uint32、uint64;和另一个模板函数(相同的名称),其代码不同,只用于FLOAT类型float32、float64。。。不管怎样,在C++中使用模板函数来完成它吗? 例如: template <class T> void fun(T b) { /// Code for integer type } template <class S>

我想写一个模板函数,当类型为整数时应该调用它,例如int8、int16、int32、int64、uint8、uint16、uint32、uint64;和另一个模板函数(相同的名称),其代码不同,只用于FLOAT类型float32、float64。。。不管怎样,在C++中使用模板函数来完成它吗? 例如:

template <class T>
void fun(T b)
{
   /// Code for integer type
}


 template <class S>
void fun(S a)
{
   /// Code for floating type
}
模板
虚无乐趣(T b)
{
///整数类型的代码
}
样板
虚无乐趣(S a)
{
///浮动类型代码
}

是的,这是可能的。一种方法是使用:


是的,这是可能的。一种方法是使用:


根据您的全部意图,除了和之外,您还可以使用常规函数重载和/或模板专门化:

#include <iostream>
#include <cstdint>

template < typename T >
void fun(T b)
{
    std::cout << "Generic called: " << b << std::endl;
}

void fun(int16_t b)
{
    std::cout << "int16_t: " << b << std::endl;
}

void fun(int32_t b)
{
    std::cout << "int32_t: " << b << std::endl;
}

void fun(float b)
{
    std::cout << "float: " << b << std::endl;
}

template <>
void fun<int64_t>(int64_t b)
{
    std::cout << "int64_t specialization: " << b << std::endl;
}

int main(int argc, char** argv)
{
    double dbl = 3.14159;
    float flt = 1.12345;
    int16_t i16 = 16;
    int32_t i32 = 32;
    fun(dbl);
    fun(flt);
    fun(i16);
    fun(i32);
    fun(5.555f);
    fun(32);
    fun(std::numeric_limits<int64_t>::max());
    return 0;
}
#包括
#包括
模板
虚无乐趣(T b)
{

std::cout根据您的全部意图,除了and之外,您还可以使用常规函数重载和/或模板专门化:

#include <iostream>
#include <cstdint>

template < typename T >
void fun(T b)
{
    std::cout << "Generic called: " << b << std::endl;
}

void fun(int16_t b)
{
    std::cout << "int16_t: " << b << std::endl;
}

void fun(int32_t b)
{
    std::cout << "int32_t: " << b << std::endl;
}

void fun(float b)
{
    std::cout << "float: " << b << std::endl;
}

template <>
void fun<int64_t>(int64_t b)
{
    std::cout << "int64_t specialization: " << b << std::endl;
}

int main(int argc, char** argv)
{
    double dbl = 3.14159;
    float flt = 1.12345;
    int16_t i16 = 16;
    int32_t i32 = 32;
    fun(dbl);
    fun(flt);
    fun(i16);
    fun(i32);
    fun(5.555f);
    fun(32);
    fun(std::numeric_limits<int64_t>::max());
    return 0;
}
#包括
#包括
模板
虚无乐趣(T b)
{
std::cout根据,我们可以在模板替换阶段为一种类型设计编译错误,而让另一种类型去编译。在整数类型与浮点类型的情况下,我们可以对整数使用位相关运算符,如
~
。例如

#include <iostream>

template <typename T, T=~0>
void f_(T a, int)  // additional tags for unambiguous overloading, same below
{
    std::cout << a << " is of integral type." << std::endl;
}

template <typename T>
void f_(T a, char)
{
    std::cout << a << " is of floating-point type." << std::endl;
}

template <typename T>
void f(T a)
{
    f_<T>(a, 0);
}


int main()
{
    f<char>('a');
    f<int>(0);
    f<float>(1.5f);
    f<double>(2.5);
    return 0;
}
根据,我们可以在模板替换阶段为一种类型设计编译错误,而让另一种类型去编译。在整数类型与浮点类型的情况下,我们可以对整数使用位相关运算符,如
~
。例如

#include <iostream>

template <typename T, T=~0>
void f_(T a, int)  // additional tags for unambiguous overloading, same below
{
    std::cout << a << " is of integral type." << std::endl;
}

template <typename T>
void f_(T a, char)
{
    std::cout << a << " is of floating-point type." << std::endl;
}

template <typename T>
void f(T a)
{
    f_<T>(a, 0);
}


int main()
{
    f<char>('a');
    f<int>(0);
    f<float>(1.5f);
    f<double>(2.5);
    return 0;
}

相关的,但有一些技术读起来更好:只需使用重载-这就是它们for@pm100我不建议有人为所有整数和2个浮点类型编写所有重载。这是很多不必要的代码重复经验(和时间压力)我教过我选择一种类型并坚持使用它。当然,如果你是一个图书馆作家,事情可能会有所不同。使用SFINAE。一个使用
std::is_integral
!is_floating_point
,另一个使用reverseregated,但是有一些技术读起来更好:只使用重载——这就是它们for@pm100我会ldn不建议有人为所有整数和2个浮点类型编写所有重载。这是很多不必要的代码重复经验(和时间压力)我已经教过我选择一种类型并坚持使用它。当然,如果你是一名图书馆作家,事情可能会有所不同。使用SFINAE。一种使用
std::is_integral
!is_floating_point
,另一种使用相反的方法,当你想通过
int64_t
uint8来重载所有的
int8_t
_t
通过
uint64\u t
,这个解决方案有很多代码重复。但是,当我遇到这种情况时,我通常会发现它是不必要的,我可以只重载
int64\u t
uint64\u t
,并称之为完成。我完全同意,@Justin!!OP的问题暗示他们需要单独的功能但是,对于每种类型,都要使用ON,这就是为什么建议保留简单的函数重载,如果这确实是他们的意图。对于您希望通过
int64\u t
uint8\u t
通过
uint64\u t
重载所有
int8\u t
的情况,此解决方案有大量代码重复。然而,在这种情况下,我通常认为这是不必要的,我可以只重载
int64_t
uint64_t
并称之为完成。我完全同意,@Justin!!OP的问题暗示他们希望每种类型都有单独的函数,这就是为什么如果这确实是他们的意图,建议保留简单的函数重载。
a is of integral type.
0 is of integral type.
1.5 is of floating-point type.
2.5 is of floating-point type.