From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2265 invoked by alias); 24 Jan 2009 13:32:33 -0000 Received: (qmail 2247 invoked by uid 48); 24 Jan 2009 13:32:18 -0000 Date: Sat, 24 Jan 2009 13:32:00 -0000 Subject: [Bug c++/38958] New: 'unused variable' warning emitted when extending the lifetime of a returned RAII type by holding a reference to const despite delayed destructor side-effects. [dtor] X-Bugzilla-Reason: CC Message-ID: Reply-To: gcc-bugzilla@gcc.gnu.org To: gcc-bugs@gcc.gnu.org From: "fs dot open dot work at googlemail dot com" Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2009-01/txt/msg02646.txt.bz2 Given a class C with destructor side-effects (as is common with RAII classes) and a function fn which returns a rvalue of type C C fn() the lifetime of the rvalue returned by fn() can be extended by holding a reference to const C with C const& r = fn() Although r is not explicitly referenced subsequently in the calling scope, upon leaving scope the rvalue's dtor will be invoked. So r is implicitly 'used'. The assignment C c = fn() has the same effect because of return value optimization. Though copy-semantics must be checked, the copy is elided. G++ generates an 'unused variable' warning for 'r' but not for 'c'. The following program demonstrates the warning. The 'volatile int g' and forced-inline noise is to compare the generated assembly to that generated when the const& is removed. The output in both cases is equivalent (identical at -O2; the only difference in lower optimizations is an additional indirection with the const& present). ---------------------------------- volatile int g; // for obvious side-effect visibility in generated assembly struct Lock { __attribute__((always_inline)) // for obvious side-effect visibility in generated assembly ~Lock() { g = 0; } }; Lock AcquireLock() { return Lock(); } int main() { Lock const& lock = AcquireLock(); g = 1; g = 2; g = 3; } ---------------------------------- In both cases, const& present and not present, the sequence of values moved into g are 1,2,3,0 as desired. This might be related to Bug 10416 but does not appear to duplicate it since removing the const& removes the warning in this case. This warning can be avoided by relying on return-value optimization and using the assignment without the const& to generate equivalent code but I don't think it should be emitted at all if it is not for the RVO version. I would expect it emitted for both or not emitted for either. -- Summary: 'unused variable' warning emitted when extending the lifetime of a returned RAII type by holding a reference to const despite delayed destructor side-effects. [dtor] Product: gcc Version: 4.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: fs dot open dot work at googlemail dot com GCC build triplet: i686-pc-linux-gnu, mingw GCC host triplet: i686-pc-linux-gnu, mingw GCC target triplet: i686-pc-linux-gnu, mingw, arm-xscale-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38958