The current C++ standard draft in [class.temporary]/7 contains the phrase
a temporary object other than a function parameter object
I was under the impression that function parameter objects are not temporary objects. However, this phrase was added relatively recently. So am I mistaken or misunderstanding the context?
An example in [stmt.ranged] illustrates CWG's intent:
using T = std::list<int>;
const T& f1(const T& t) { return t; }
const T& f2(T t) { return t; }
T g();
void foo() {
for (auto e : f1(g())) {} // OK, lifetime of return value of g() extended
for (auto e : f2(g())) {} // undefined behavior
}
So the "other than a function parameter object" wording is referring to the parameter t of f2.
I think the OP is right that this isn't actually a temporary object; it doesn't fall under any of the categories mentioned in [class.temporary]/1. However, on some implementations a function parameter object has temporary-like characteristics in that it might be destroyed at the end of the full-expression containing the call, rather than at the point you would expect for a block variable. And I think that was the case that CWG was trying to address here.
Scrolling up to [class.temporary]/3:
implementations are permitted to create a temporary object to hold the function parameter
Not all parameter objects are temporary objects, but some are allowed to be.
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