简体   繁体   English

在C ++中,是否可以将两种不同的数据类型传递给模板函数?

[英]In C++, is it possible to pass in two different data types to a template function?

I'm trying to calculate the absolute value of two numeric values passed in by a user, but allowing the user to enter multiple data types (ie an integer and a double, or a char and a float). 我正在尝试计算用户传入的两个数值的绝对值,但允许用户输入多种数据类型(即,整数和双精度数,或char和浮点数)。 My initial thought is to use a function sort of like this: 我最初的想法是使用类似这样的函数:

template <class T1, class T2>
void findAbs(const T1& var1, const T2& var2)
{
    cout<<"Enter two numbers: "<<endl;
    cin>>var1>>var2;

   cout<<abs(var1)<<" "<<abs(var2)<<endl;
}

If this is the correct way to do it though, I have no idea how I'd call it in the main function since it seems I'd have to declare the parameters as one data type or another. 如果这是正确的方法,那么我不知道如何在主函数中调用它,因为似乎必须将参数声明为一种或另一种数据类型。 Any help would be much appreciated! 任何帮助将非常感激!

First off, your example won't compile, because you're taking the parameters by const-reference, and then trying to read into them from the stream. 首先,您的示例将无法编译,因为您将通过const-reference来获取参数,然后尝试从流中读取它们。 You should take them by non-const reference instead. 您应该改为通过非常量引用来获取它们。

With this fixed, you could simply use the function like this: 修复此问题后,您可以简单地使用如下功能:

int main()
{
  int i;
  float f;
  double d;
  char c;

  findAbs(i, f);
  findAbs(c, d);
  findAbs(d, i);
  //etc.
}

Of course, the type of the arguments must be known in each call site. 当然,必须在每个调用站点中知道参数的类型。 Templates are a purely compile-time construct. 模板是纯粹的编译时构造。 If you were hoping to somehow use the template to differentiate between the end-user typing c , 42 or -3.14 , you can't, as that's run-time information. 如果您希望以某种方式使用模板来区分最终用户键入c42-3.14 ,则不能,因为那是运行时信息。

I have no idea how I'd call it in the main function since it seems I'd have to declare the parameters as one data type or another. 我不知道如何在main函数中调用它,因为似乎我不得不将参数声明为一种数据类型或另一种数据类型。

No, template parameters can be deduced in this case. 不可以,在这种情况下可以推导出模板参数。 But the main problem is that operator>> of std::cin modifies the parameters, therefore you shouldn't declare them const : 但是主要的问题是std::cin operator>>修改了参数,因此您不应将它们声明为const

template <class T1, class T2>
void findAbs(T1& var1, T2& var2) {
    std::cout << "Enter two numbers: " << std::endl;
    std::cin >> var1 >> var2;
    std::cout << std::abs(var1) << ' ' << std::abs(var2) << std::endl;
}

Then you should be able to call: 然后,您应该可以致电:

int x, y;
findAbs(x, y);

It might be wise to consider using a single template parameter for both of the arguments though. 不过,考虑为两个参数使用单个模板参数可能是明智的。

Yes, but it depends on whether or not the types you pass in would lead to a piece of code which is valid. 是的,但这取决于您传入的类型是否会导致一段有效的代码。 Expand the template in your head (or type it out, whatever). 将模板展开到您的头部(或键入,随便键入)。 Does calling abs on variables of type T1 and T2 make sense? T1T2类型的变量上调用abs是否有意义? Would it compile? 会编译吗? How about the calls to cin ? 打电话给cin怎么样? If so, then yes, your code will work. 如果是这样,那么是的,您的代码将起作用。

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

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