java声明一个函数_如何声明一个推导出其返回类型的函数?

根据提交的论文,N3638,明确有效 .

相关片段:

auto x = 5; // OK: x has type int

const auto *v = &x, u = 6; // OK: v has type const int*, u has type const int

static auto y = 0.0; // OK: y has type double

auto int r; // error: auto is not a storage-class-specifier

auto f() -> int; // OK: f returns int

auto g() { return 0.0; } // OK: g returns double

auto h(); // OK, h's return type will be deduced when it is defined

然而它继续说:

如果需要具有未减弱占位符类型的实体类型来确定表达式的类型,则程序格式错误 . 但是一旦在函数中看到了return语句,从该语句推导出的返回类型可以在函数的其余部分中使用,包括在其他return语句中 .

auto n = n; // error, n's type is unknown

auto f();

void g() { &f; } // error, f's return type is unknown

auto sum(int i) {

if (i == 1)

return i; // sum's return type is int

else

return sum(i-1)+i; // OK, sum's return type has been deduced

}

因此,在定义之前使用它的事实会导致错误 .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值