C++ Logo

std-discussion

Advanced search

Re: is this valid static const reference

From: Thiago Macieira <thiago_at_[hidden]>
Date: Wed, 08 Jul 2020 18:49:34 -0700
On Wednesday, 8 July 2020 17:55:09 PDT spsaxena via Std-Discussion wrote:
> Foo dup() { printf("dup foo %p\n", this); return *this; }
> };
> static const Foo& tempPath(Foo().dup());

Yes, that's valid. Foo::dup() returns an xvalue, so that gets lifetime-
extended until program termination and that object is stored in "tempPath".

Suggestion: drop the reference and save one indirection.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel System Software Products

Received on 2020-07-08 20:52:53