void foo(const std::string& s = "abc") {
// ...
}
// ...
int main() {
// ...
foo();
// ...
}
Will s
in foo
be dangling? I think because std::string
will be constructed from default value "abc"
, and then this will be a const reference do died temporary.
Am I right?
Not just a copy; it is also a const copy. So you cannot modify it, invoke any non-const members from it, or pass it as a non-const parameter to any function.
Passing by const reference allows changes to the original object to be visible inside the function.
Const Reference to a pointer is a non-modifiable value that's used same as a const pointer. Here we get a compile-time error as it is a const reference to a pointer thus we are not allowed to reassign it.
But const (int&) is a reference int& that is const , meaning that the reference itself cannot be modified.
s
will not dangle in foo
, but the temporary will live for the entirety of foo
. There are a couple pieces that need to be understood to understand why this happens:
When you declare a default argument on the function, the default argument is inserted at the call site. The code you wrote behaves the same as the following code:
void foo(const std::string& s) {
// ...
}
// ...
int main() {
// ...
foo("abc");
// ...
}
So the std::string
temporary is created at the call site.
When the temporary std::string
is bound to the const std::string& s
, the temporary is lifetime extended. It will live until the end of the complete expression, i.e. the semicolon at the end of foo("abc");
.
Putting this together, we can see that s
will not dangle, because it points to a temporary string which will live at least as long as foo
will execute.
The constructor for std::string(const char*)
will be used to construct a temporary that will live for the whole lifetime of the function.
So there will be no problems.
No, the lifetime of the temporary will be extended until the evaluation of the expression containing the call to foo
ends. If s
scape the function body then it will be a dangling reference.
in standardese [class.temporary]/6.9
A temporary object bound to a reference parameter in a function call (8.2.2) persists until the completion of the full-expression containing the call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With