Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are function parameter objects temporary objects?

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?

like image 544
user17732522 Avatar asked Apr 28 '26 17:04

user17732522


2 Answers

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.

like image 173
Brian Bi Avatar answered May 01 '26 07:05

Brian Bi


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.

like image 28
JaMiT Avatar answered May 01 '26 06:05

JaMiT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!