C++ 如果我在构造函数中写 return 语句怎么办?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5255777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:46:31  来源:igfitidea点击:

What if I write return statement in constructor?

c++constructorreturn-valuereturnreturn-type

提问by Nawaz

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

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

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

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

上面的代码编译得很好,ideone没有任何错误。但以下代码没有:

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

It gives this error at ideone:

它在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 explicitlymention return type, and we cannot store the returned value after all. But I'm curious to know :

我知道从构造函数返回值根本没有意义,因为它没有明确提及返回类型,而且我们毕竟无法存储返回值。但我很想知道:

  • Which statement from the C++ Standard allows the first example but forbids the second one? Is there any explicitstatement?
  • Is the return typein the first example void?
  • Is there any implicitreturn type at all?
  • C++ 标准中的哪个语句允许第一个示例但禁止第二个示例?有什么明确的说法吗?
  • 是第一个示例中的返回类型void吗?
  • 是否有任何隐式返回类型?

回答by AnT

Yes, using return statements in constructors is perfectly standard.

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

Constructors are functions that do not return a value. The family of functions that do not return a valueconsists 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 returnwith an argument in a function that does not return a value.

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

6.6.3 The return statement

2A 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.

6.6.3 返回语句

2不带表达式的 return 语句只能用于不返回值的函数,即返回类型为 void、构造函数 (12.1) 或析构函数 (12.4) 的函数。带有非 void 类型表达式的 return 语句只能在返回值的函数中使用;表达式的值返回给函数的调用者。

Additionally, 12.1/12 states that

此外,12.1/12 指出

12.1 Constructors

12No 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.

12.1 构造函数

12不应为构造函数指定返回类型(甚至无效)。构造函数体中的 return 语句不应指定返回值。

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

请注意,顺便说一句,在 C++ 中,return在 void 函数中使用参数是合法的,只要 的参数return具有类型void

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

This is not allowed in constructors though, since constructors are not void functions.

但是,这在构造函数中是不允许的,因为构造函数不是空函数。

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

还有一个return与 with 构造函数的使用相关的相对模糊的限制:在构造return函数的 function-try-block 中使用是非法的(与其他函数一起使用是可以的)

15.3 Handling an exception

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

15.3 处理异常

15如果 return 语句出现在构造函数的函数 try 块的处理程序中,则程序格式错误。

回答by Shamim Hafiz

Perhaps the notion of having typeless return in constructors is to control the termination of constructor function.

也许在构造函数中具有无类型返回的概念是为了控制构造函数的终止。

struct A
{ 
// more definitions     
A() 
{ 
if( !goodToGoOn)  
 return;
// the rest of the stuffs go here
} 
};