I was wondering and couldn't find a reliable source to answer my question : Does a construtor implicitly return the value of the type of the object itself ?
my sources : https://blog.miyozinc.com/core-tutorials/cpp/cpp-constructors-destructors/ (does return)
Does copy constructors have return value in C (does not return)
CodePudding user response:
No.
As per [class.ctor]/6:
A return statement in the body of a constructor shall not specify a return value. The address of a constructor shall not be taken.
And, similarly [stmt.return]/1 and [stmt.return]/2:
/1 A function returns to its caller by the return statement.
/2 [...] A return statement with no operand shall be used only in a function whose return type is cv void, a constructor, or a destructor. [...] Flowing off the end of a constructor, a destructor, or a non-coroutine function with a cv void return type is equivalent to a return with no operand.
Constructors do not have return values, but after their completion they return to its caller.
struct S {
S(int) { }
};
int main() {
S s{42};
}
is equivalent to
struct S {
S(int) { return; }
};
int main() {
S s{42};
}
Finally, return values are relevant in the context of function calls, but none of S s{42}, S{42}, S s(42) or S(42) is a function call, it is initialization for the case where an initializer follows a declarator.
CodePudding user response:
No. A constructor creates an object in a specific location. This location is names this in the constructor, just as it's name this in the destructor and all other member functions. this might be considered an "implicit argument" to all member functions, but this is not an "implicit return value" of any member function.
