函数声明后的“ - >”是什么?

这种语法的优势之一是可以在函数的返回类型中使用函数参数,使得返回类型更灵活。

先来看一个使用尾返回类型的例子:

#include <iostream>
auto add(int a, int b) -> int
{
    return a + b;
}

int main() 
{
    std::cout << add(3, 4) << std::endl;  // 输出 7
    return 0;
}

在这个例子中,add 函数声明使用了尾返回类型,通过 -> int 指定了函数的返回类型为 int。这种语法的优势之一是可以在函数的返回类型中使用函数参数,使得返回类型更灵活。

需要注意的是,尾返回类型通常在函数定义的时候使用,而不是在函数声明的时候。

这是来自C++ 11的新函数声明语法,它被称为“尾随返回类型”。在函数声明结束时,->表示以下是该函数的返回类型。只有在使用auto关键字而不是您通常期望的实际返回类型时才能使用它。

举例来说,这两个声明是兼容的:

int foo(); 
auto foo() -> int;

根据你的口味,你会发现它比旧的声明语法漂亮,尤其是当返回类型是非常长/复杂:

task<typename details::_TaskTypeFromParam<_Ty>::_Type> create_task(_Ty _Param); 
auto create_task(_Ty _Param) -> task<typename details::_TaskTypeFromParam<_Ty>::_Type>;

但是有时可能需要使用模板,当函数的返回类型可能随参数而变化时。

说你想要一个模板函数添加变量:

template<typename T> 
T add(const T& x, const T& y) 
{ 
    return x + y; 
}

你只能添加相同类型的变量。假设您希望能够添加任何类型的变量(如add((int)1, (double)2))。

template<typename T, typename U> 
??? add(const T& x, const U& y) 
{ 
    return x + y; 
}

问题是你不能事先告诉x + y的结果类型将是什么。作为模板,它们甚至可以是非整数类型。(难道你不能这样做add(std::string("x"), "y")?)

Decltype,以及新的函数声明语法,让你解决这个问题。

template<typename T, typename U> 
auto add(const T& x, const U& y) -> decltype(x + y) 
{ 
    return x + y; 
}

Decltype“返回”表达式的类型。由于您需要xy已宣布为decltype(x + y)才能正常工作,因此您需要新的语法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这段代码存在一些问题,例如: 1. `print`函数没有在程序之前声明,需要加上函数声明。 2. `print`函数的`printf`语句没有换行符,需要加上换行符。 3. `main`函数的`print`函数调用没有加上参数,需要传入链表头节点。 4. `print`函数和`main`函数的`printf`语句格式不一致,需要保持一致。 5. `print`函数和`main`函数的`scanf`函数格式不一致,需要保持一致。 6. `create`函数的`p->next`应该指向`head->next`,而不是`head`本身。 以下是修改后的代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char xm[20]; char xb; int cj; struct node* next; }; void print(struct node* head); struct node* create(int n) { struct node* head, * p; head = (struct node*)malloc(sizeof(struct node)); head->next = NULL; int i; for (i = 0; i < n; i++) { p = (struct node*)malloc(sizeof(struct node)); scanf("%s %c %d", p->xm, &p->xb, &p->cj); p->next = head->next; head->next = p; } return head; } int main() { int n; scanf("%d", &n); struct node* pr; pr = create(n); print(pr->next); while (pr->next != NULL) { if (strcmp(pr->xm, "zhang") == 0 && pr->cj >= 90) { printf("%s %c %d\n", pr->xm, pr->xb, pr->cj); } pr = pr->next; } return 0; } void print(struct node* head) { struct node* tem; tem = head; while (tem != NULL) { printf("%s %c %d\n", tem->xm, tem->xb, tem->cj); tem = tem->next; } } ``` 修改后的程序,`print`函数在程序之前进行了声明,函数的`printf`语句加上了换行符。`main`函数调用`print`函数时传入了链表的第一个节点。 将`create`函数的`p->next`指向了`head->next`,保证了新节点插入到链表头部。 在`main`函数,将字符串`"zhang"`与姓名进行比较时,使用了`strcmp`函数来比较字符串。 最后,所有的`printf`语句都保持了一致的格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Risehuxyc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值