如果我在构造函数中写 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 给出了这个错误:

It gives this error at 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 中返回的是 type 吗?
  • 是否有任何隐式返回类型?
  • 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 没有返回类型(甚至无效)为构造函数指定.一个主体中的 return 语句构造函数不应指定返回价值.

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.

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

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)
}

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

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

还有一个相对模糊的限制与 return 与构造函数的使用相关:在构造函数的函数尝试块中使用 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 如果 return 语句出现在函数 try 块的处理程序构造函数,程序格式错误.

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

相关文章