模板参数作为返回类型 [英] Template parameter as return type

查看:66
本文介绍了模板参数作为返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我前段时间偶然发现了这个问题,我终于需要得到一个答案!

I stumbled upon this some time ago and I finally need to get an answer to this!

问题如下:

#include <iostream>

using namespace std;

template <class FIRST, class SECOND>

FIRST multiply(FIRST a, SECOND b){
    return a * b;
}

int main(){
    int x = 39;
    double y = 38.38;
    cout << multiply(x, y) << endl;
}

在这个 C++ 代码中,我有两个模板参数,函数的返回类型是一个参数的类型.我的问题是返回类型必须(在这种情况下)与变量 a 是相同的类型.我用第三个模板参数尝试过它,但它给了我一个编译错误,谁能给我解释一下?

In this C++ code I have two template parameters and the return type of the function is the type of one parameter. My problem with that is that the return type has to be (in this case) the same type like the variable a is. I tried it with a THIRD template parameter but it gave me an compiling error, could anyone please explain this to me?

我不想让它成为一个 long 或其他东西,我希望返回类型是传递的变量的最大"类型,以便它可以处理字符和字符串还有(对于那些我显然会做其他操作或其他事情的人,这只是一个例子).

I don't want to make it generally a long or something, I want the return type to be the 'biggest' type of the variables passed, so that it will work with chars and strings as well (for those I would obviously do another operation or something, this is only an example).

推荐答案

处理此类情况的规范方法是返回从调用的操作结果返回的适当类型:

The canonical approach to deal with situations like this is to return the appropriate type returned from the result of the operation called:

template <typename FIRST, typename SECOND>
auto multiply(FIRST a, SECOND b) -> decltype(a * b) {
    return a * b;
}

这个用例几乎是添加延迟返回类型的激励示例:操作的结果类型可能完全不可预测.这导致了两个需求:

This use-case is pretty much the motivating example for adding late return types: the result type of an operation can be entirely non-predictable. This leads to two needs:

  1. 需要能够确定表达式的类型,这就是 decltype(expr) 所做的:它产生 expr 声明的类型:当 expr 是函数调用时,您会查看函数声明,这就是 decltype(expr) 产生的结果(好吧,其中有一些复杂性).
  2. 要实际制定expr,通常需要掌握函数参数.因此,引入了一种新的函数声明方法:

  1. It needs to be possible to determine the type of an expression which is what decltype(expr) does: it yields the type of what expr is declared to be: when expr is a function call you'd look at the function declaration and that's what decltype(expr) yields (well, there are some complexities in there).
  2. To actually formulate expr it is frequently necessary to get hold of function arguments. Thus, a new approach to declare function was introduced:

auto function-name(args) -> 返回类型

auto 关键字仅表示该函数将使用延迟返回类型.function-nameargs 的用法与其他 functino 声明中的用法相同.return-type 然后跟在可以使用参数名称的地方,即在 -> 之后是 return->type它通常位于函数声明的前面.

The auto keyword is merely indicating that the function will use a late return type. The function-name and args are used the same as in other functino declaration. The return-type then follows at a point where parameter names can be used, i.e., after the -> is the return->type which is is normally in front of the function declaration.

换种说法,如果return-type引用参数名称,则上面的声明等价于这个声明:

Put differently, if return-type does not refer to the names of arguments, the declaration above is equivalent to this declaration:

返回类型 函数名(args)

如果 return-type 引用在 args 中引入的名称,您需要处理获取适当类型的对象,例如,使用 std::declval().

If return-type refers to a name introduced in args you'd need to deal with getting object of the appropriate types, e.g., using std::declval<T>().

这篇关于模板参数作为返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆