Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What if I write return statement in constructor?

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

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

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

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

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 :

  • 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?
like image 420
Nawaz Avatar asked Mar 10 '11 05:03

Nawaz


People also ask

Can we write return statement in constructor?

You cannot declare a constructor as virtual or static , nor can you declare a constructor as const , volatile , or const volatile . You do not specify a return type for a constructor. A return statement in the body of a constructor cannot have a return value.

What will happen if we write return type for constructor?

If we add a return type to a constructor, then it will become a method of the class. This is the way java runtime distinguish between a normal method and a constructor.

Can we use return statement in constructor in java?

There are no “return value” statements in the constructor, but the constructor returns the current class instance. We can write 'return' inside a constructor.

Does a constructor need a return statement?

Usually, constructors do not have a return statement. Their task is to write all necessary stuff into this , and it automatically becomes the result.


2 Answers

Yes, using return statements in constructors is perfectly standard.

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 The return statement

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.

Additionally, 12.1/12 states that

12.1 Constructors

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.

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.

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 Handling an exception

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

like image 173
AnT Avatar answered Oct 19 '22 19:10

AnT


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
} 
};
like image 45
Shamim Hafiz - MSFT Avatar answered Oct 19 '22 17:10

Shamim Hafiz - MSFT