简体   繁体   English

具有灵活返回类型的C ++函数模板

[英]C++ Function Template With Flexible Return Type

Let's say that we have a function like so 让我们说我们有这样的功能

template <class T, class T2>
T getMin(T a, T2 b) {
  if(a < b)
    return a;
  return b;
}

if we call the function like so 如果我们这样调用函数

int a, b;
long c;

a = getMin(b, c);

if c is < a, then the value of c will be type casted to int. 如果c是<a,那么c的值将被类型转换为int。

Is it possible to make the return type flexible so that it would return an int, or long, or any other type considered smaller by "<" without being type casted? 是否可以使返回类型具有灵活性,以便返回int,long或任何其他类型的“<”而不是类型的类型?

edit : the type involved in the function can be anything from simple type to complex classes where typecasting sometime won't be possible. 编辑:函数中涉及的类型可以是从简单类型到复杂类的任何类型,其中某些时候无法进行类型转换。

C++0x will allow you to use the auto keyword in order to let the compiler derive the return time of an expression. C ++ 0x将允许您使用auto关键字,以便让编译器派生表达式的返回时间。


For C++03 the only way I found to automatize such process is to define a template class Promotion that defines the strongest type between two types, and then specialize it for any couple of types you might need to use. 对于C ++ 03,我发现自动化此类过程的唯一方法是定义一个模板类Promotion ,它定义两种类型之间的最强类型,然后将它专门用于您可能需要使用的任何类型。

template<> class Promotion< long, int > { typedef long strongest; }
template<> class Promotion< int, long > { typedef long strongest; }

and thus: 因此:

template< typename T1, typename T2 >
Promotion<T1,T2>::strongest function( const T1 &a, const T2 &b ) { ... }

If you choose to try this solution, I'd suggest to generate the Promotion specializations with an automatically generated header file. 如果您选择尝试此解决方案,我建议使用自动生成的头文件生成促销专精。


Edit: I reread the question after reading the other (now deleted) answer: 编辑:我在阅读其他(现已删除)答案后重新阅读了该问题:

You can't return the type of the smaller variable. 您无法返回较小变量的类型。 That's because the value of the variables will only be found out at runtime, while your function return type must be defined at compile time. 那是因为变量的值只能在运行时找到,而函数返回类型必须在编译时定义。

The solution I proposed will return always the strongest type between the two variables' type. 我提出的解决方案将始终返回两个变量类型之间的最强类型。

As has been said, you want to take the type that is more generic. 如前所述,您希望采用更通用的类型。 Like, int and double should become double ; 比如, intdouble应该变成double ; char* and string should become string . char*string应该成为string This works with my promote<> template. 这适用于我的promote<>模板。 Just write 写吧

template <class T1, class T2>
typename promote<T1, T2>::type getMin(T1 const& a, T2 const& b) {
  if(a < b)
    return a;
  return b;
}

This will always return a copy, even if T1 and T2 is the same type (like string ), which is why I would overload it for non-const same-type arguments 这将始终返回一个副本,即使T1T2是相同的类型(如string ),这就是为什么我会为非const相同类型的参数重载它

template <class T>
T &getMin(T &a, T &b) {
  if(a < b)
    return a;
  return b;
}

These two variants seem to be a reasonable configuration. 这两个变体似乎是一个合理的配置。 If you want to have a slightly more risky, but in more cases performant, solution, you can accept T const& , also accepting temporaries. 如果你想要有更高的风险,但在更多情况下,性能,解决方案,你可以接受T const& ,也接受临时。 If you then use it like getMin(a + b, b + c) , which could pass temporaries, and use the result directly, that's all fine. 如果你然后使用它像getMin(a + b, b + c) ,它可以通过临时,并直接使用结果,这一切都很好。 The result is usable and can still be copied into a local variable. 结果可用,仍可以复制到局部变量中。

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

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