A unique_ptr returned by the function has no scope because the scope applies only to names.
In your example, the lifetime of the temporary unique_ptr ends at a semicolon. (So ββyes, this will work correctly.) In general, a temporary object is destroyed when a full expression that lexically contains an rvalue whose evaluation created this temporary object is fully evaluated.
source share