简体   繁体   English

在C ++中使用模板专业化

[英]Using template specialization in C++

How can I write a function using template specialization that has 2 different input types and an output type: 如何使用具有两种不同输入类型和输出类型的模板专业化来编写函数:

template <class input1, class input2, class output>

and return the sum of the 2 numbers (integers/doubles). 并返回两个数字的总和(整数/双精度)。 However, if I get 2 integers I want to return an integer type but for any other combinations of integer and double I'll always return double. 但是,如果我得到2个整数,我想返回一个整数类型,但是对于整数和double的任何其他组合,我将始终返回double。

I am trying to do that without using directly the '+' operator but having the next functions instead: 我正在尝试不直接使用'+'运算符而是使用下一个函数来做到这一点:

double add_double_double(double a, double b) {return (a+b);}
double add_int_double(int a, double b) {return ((double)(a)+b);}
int   add_int_int(int a, int b) {return (a+b);}

If you can use C++0x, you could do this: 如果可以使用C ++ 0x,则可以执行以下操作:

template <typename T, typename U>
auto add(T lhs, U rhs) -> decltype(lhs+rhs)
{
    return lhs+rhs;
}

Don't use template specialization. 不要使用模板专门化。 Use overloading instead. 请改用重载。 Specializing functions is complicated and rarely needed: 专用功能非常复杂,几乎不需要:

template <typename T1, typename T2>
double sum(T1 a, T2 b) { return a + b; }

int sum(int a, int b) { return a + b; }

The second version will be called if and only if both arguments are int ; 当且仅当两个参数均为int ,才会调用第二个版本; otherwise, the first version will be called. 否则,将调用第一个版本。

This might be what you need: 这可能是您需要的:

#include <iostream>
using namespace std;

template <class input1, class input2, class output> output add(input1 n1, input2 n2) {
   return (output) n1 + (output) n2;
}

int main(int argc, char **argv) {

   cout << add<int, int, int>(1,1) << endl;
   cout << add<float, int, float>(1.1f,1) << endl;
   cout << add<int, float, float>(1,1.1f) << endl;
   cout << add<float, float, int>(1.1f,1.1f) << endl;

   return 0;
}

Result: 结果:

quad: > ./a.out 
2
2.1
2.1
2
Sun 09 Jan 2011 12:57:57 PM MST

quad: > 

The answer to your question lies in Chapter 15 of C++ templates book. 您问题的答案在C ++模板书的第15章中。 This chapter has an accumulator example which addresses your question. 本章有一个累加器示例 ,可以解决您的问题。 Further down the chapter it will talk about promotion traits where it will address the problem of adding two different types and also address type promotion. 在本章的后续部分,将讨论提升特性,它将解决添加两种不同类型的问题,并解决类型提升问题。

It is possible for the return type of a function template to depend on the types of the template parameters. 函数模板的返回类型有可能取决于模板参数的类型。 In your case, you can do something similar to: 就您而言,您可以执行以下操作:

template <typename L, typename R>
struct sum_traits { typedef double return_type; };

template <>
struct sum_traits<int, int> { typedef int return_type; };

template <typename L, typename R>
typename sum_traits<L, R>::return_type
sum(L l, R r) { 
  typedef typename sum_traits<L, R>::return_type ret_t;
  return ret_t(l) + ret_t(r); 
}

I don't think the explicit casts to ret_t are ever actually needed in this case , but it demonstrates some more of the technique. 在这种情况下 ,我认为实际上不需要显式转换为ret_t ,但是它展示了更多的技巧。

This handles just ints, but the idea can easily be generalized to handle more complex cases. 这只处理整数,但是可以很容易地将想法推广到处理更复杂的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM