首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

return statement

终止当前函数并将指定的值返回给调用方函数。

句法

attr(optional) return expression(optional) ;

(1)

?

attr(optional) return braced-init-list ;

(2)

(since C++11)

attr(C++11)

-

optional sequence of any number of attributes

expression

-

expression, convertible to the function return type

braced-init-list

-

brace-enclosed list of initializers and other braced-init-lists

解释

1%29计算表达式,终止当前函数并将表达式的结果返回给调用方。隐式转换函数返回类型。该表达式在返回类型为%28的函数中是可选的,可能是cv限定%29。void、构造函数和析构函数。

2%29次使用复制列表初始化若要构造函数的返回值,请执行以下操作。

注记

如果控件以返回类型到达函数的末尾void%28可能是cv-限定%29、构造函数结束、析构函数结束或功能试块对于返回类型为%28的函数,可能为cv---限定%29。void没有遇到返回语句,return;被处决了。

如果控件到达主要功能,,,return 0;被处决了。

流出值返回函数%28的末尾,除外main%29没有返回语句是未定义的行为。

在函数返回中void,则可以使用带表达式的返回语句(如果表达式类型为void...

按值返回可能涉及临时对象的构造和复制/移动,除非复制省略被使用了。

If expression is an lvalue expression and the conditions for copy elision are met, or would be met, except that expression names a function parameter, then overload resolution to select the constructor to use for initialization of the returned value is performed twice: first as if expression were an rvalue expression (thus it may select the move constructor or a copy constructor taking reference to const), and if no suitable conversion is available, overload resolution is performed the second time, with lvalue expression (so it may select the copy constructor taking a reference to non-const). The above rule applies even if the function return type is different from the type of expression (copy elision requires same type).

(since C++11)

The copy-initialization of the temporary that is the result of evaluating a (until C++17)the result object of the (since C++17) function call is sequenced-before the destruction of all temporaries at the end of expression, which, in turn, is sequenced-before the destruction of local variables of the block enclosing the return statement.

(since C++14)

If expression is a prvalue, the object returned by the function is initialized directly by that expression. This does not involve a copy or move constructor when the types match (see copy elision).

(since C++17)

关键词

return...

二次

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <utility>
 
void fa(int i)
{
    if (i == 2)
         return;
    std::cout << i << '\n';
} // implied return;
 
int fb(int i)
{
    if (i > 4)
         return 4;
    std::cout << i << '\n';
    return 2;
}
 
std::pair<std::string, int> fc(const char* p, int x)
{
    return {p, x};
}
 
void fd()
{
    return fa(10); // fa(10) is a void expression
}
 
int main()
{
    fa(2); // returns, does nothing when i==2
    fa(1); // prints its argument, then returns
    int i = fb(5); // returns 4
    i = fb(i); // prints its argument, returns 2
    std::cout << i << '\n'
              << fc("Hello", 7).second << '\n';
    fd();
}

二次

产出:

二次

代码语言:javascript
复制
1
4
2
7
10

二次

缺陷报告

以下行为更改缺陷报告追溯应用于先前发布的C++标准。

DR

Applied to

Behavior as published

Correct behavior

CWG 1579

C++11

return by converting move constructor was not allowed

converting move constructor look up enabled

CWG 1885

C++14

sequencing of the destruction of atomatic variables was not explicit

sequencing rules added

另见

c申报单的文件

*。

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com