如果我在构造函数中写return语句怎么办? [英] What if I write return statement in constructor?

查看:1078
本文介绍了如果我在构造函数中写return语句怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在构造函数中写return语句怎么办?是标准符合吗?

What if I write return statement in constructor? Is it standard conformant?

struct A
{ 
     A() { return; } 
};

上述代码编译正常,在ideone 。但以下代码不会:

The above code compiles fine, without any error at ideone. But the following code doesn't:

struct A
{ 
   A() { return 100; } 
};

它会在 ideone


错误:从构造函数返回值

error: returning a value from a constructor

我知道从构造函数返回值是没有意义的,因为它不是显式地提供返回类型,我们不能存储返回值毕竟。但我很好奇地知道:

I understand that returning value from constructor doesn't make sense at all, because it doesn't explicitly mention return type, and we cannot store the returned value after all. But I'm curious to know :


  • 哪个语句从C ++标准允许第一个例子,但禁止第二个?是否有任何显式语句?

  • 是第一个示例中的返回类型 void

  • 是否有任何隐含的返回类型?

  • Which statement from the C++ Standard allows the first example but forbids the second one? Is there any explicit statement?
  • Is the return type in the first example void?
  • Is there any implicit return type at all?

推荐答案

是的,在构造函数中使用return语句是完全标准的。

Yes, using return statements in constructors is perfectly standard.

构造函数是不返回值的函数不返回值的函数家族包括:void函数,构造函数和析构函数。它在C ++标准的6.6.3 / 2中说明。同样的6.6.3 / 2声明,在不返回值的函数中使用 return 是一个非法操作。

Constructors are functions that do not return a value. The family of functions that do not return a value consists of: void functions, constructors and destructors. It is stated in 6.6.3/2 in the C++ standard. The very same 6.6.3/2 states that it is illegal to use return with an argument in a function that does not return a value.


6.6.3返回语句

2 没有
表达式的返回语句只能在
函数中使用,不返回值
,也就是说,函数返回
类型void,构造函数(12.1 )或
析构函数(12.4)。返回语句
具有非void类型的表达式
只能在函数中使用
返回值;
表达式的值返回给函数的调用者

2 A return statement without an expression can be used only in functions that do not return a value, that is, a function with the return type void, a constructor (12.1), or a destructor (12.4). A return statement with an expression of non-void type can be used only in functions returning a value; the value of the expression is returned to the caller of the function.

此外,12.1 / 12表示

Additionally, 12.1/12 states that


12.1构造函数

12 没有返回类型(甚至无效)将为构造函数指定

构造函数正文中的
返回语句不应指定返回
值。

12 No return type (not even void) shall be specified for a constructor. A return statement in the body of a constructor shall not specify a return value.

注意,BTW,在C ++中,使用 return 和void函数中的参数是合法的,只要 code>有类型 void

Note, BTW, that in C++ it is legal to use return with an argument in a void function, as long as the argument of return has type void

void foo() {
  return (void) 0; // Legal in C++ (but not in C)
}

还有一个相对模糊的限制与 return 的使用相关,与构造函数:在构造函数的函数try块中使用 return 是非法的(与其他函数是一样的)

There's also one relatively obscure restriction relevant to the usage of return with constructors: it is illegal to use return in function-try-block of a constructor (with other functions it is OK)


15.3处理异常

15
构造函数的function-try-block的
处理程序中,程序不成立。

15 If a return statement appears in a handler of the function-try-block of a constructor, the program is ill formed.

这篇关于如果我在构造函数中写return语句怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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