c++language-lawyertemporary-objects

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?


Solution

  • 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.