From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 1174 invoked by alias); 15 Mar 2008 02:28:27 -0000 Received: (qmail 1165 invoked by uid 22791); 15 Mar 2008 02:28:26 -0000 X-Spam-Check-By: sourceware.org Received: from fmmailgate02.web.de (HELO fmmailgate02.web.de) (217.72.192.227) by sourceware.org (qpsmtpd/0.31) with ESMTP; Sat, 15 Mar 2008 02:28:04 +0000 Received: from smtp05.web.de (fmsmtp05.dlan.cinetic.de [172.20.4.166]) by fmmailgate02.web.de (Postfix) with ESMTP id 2581AD4B0176 for ; Sat, 15 Mar 2008 03:28:02 +0100 (CET) Received: from [213.101.239.18] (helo=[192.168.1.1]) by smtp05.web.de with asmtp (TLSv1:AES256-SHA:256) (WEB.DE 4.109 #226) id 1JaM7t-0007ke-00 for gcc-help@gcc.gnu.org; Sat, 15 Mar 2008 03:28:02 +0100 From: Lukas Mai To: gcc-help@gcc.gnu.org Subject: C++0x rvalue refs get clobbered somehow Date: Sat, 15 Mar 2008 02:28:00 -0000 User-Agent: KMail/1.9.7 MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Message-Id: <200803150326.44793.l.mai@web.de> X-Sender: l.mai@web.de Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2008-03/txt/msg00146.txt.bz2 Hello! I'm having problems with g++-4.3.0 and C++0x. Disclaimer: I don't understand rvalue references, but since I need (indirect) argument forwarding, I copied and modified an example from the web. Here's (a simplified version of) my code. Details like destructors, dynamic memory management, etc are gone; it's just generic object construction. ------------------------------------------------------------------ #include #include namespace awesome { namespace detail_region { template struct Typedef { typedef T t; }; template T &&fw(typename Typedef::t &&x) { return x; } template struct tuple; template<> struct tuple<> { template T *capply(void *place, Args &&...args) const { return new (place) T(fw(args) ...); } }; template struct tuple { Hd &&head; tuple tail; tuple(Hd &&h, Tl &&...t) : head(fw(h)), tail(fw(t) ...) {} template T *capply(void *place, Args &&...args) const { return tail.capply(place, fw(args) ..., fw(head)); } }; } struct region { private: template struct spork { detail_region::tuple args; spork(Args &&...x) : args(detail_region::fw(x) ...) {} template operator T *() const { // ::operator new(1); // std::cout << "hi\n"; std::rand(); // XXX any function call here seems to cause problems static char raw_[sizeof (T)]; return args.template capply(raw_); } }; public: template spork alloc(Args &&...args) { return spork(detail_region::fw(args) ...); } }; } using namespace std; int main() { using awesome::region; region r; int *p = r.alloc(123); cout << *p << '\n'; } ------------------------------------------------------------------ This code works fine (and prints 123) when compiled with g++ -std=c++0x -O2 (or -Os or -O). However, without optimization it produces output like -1207981096. This number changes if different function calls are used at the location marked XXX. Somehow args.head loses its value between construction (in alloc) and use (in capply), but only if optimizations are not enabled. This is obviously a bug, either in my code or in g++. I think args.head should be bound to the temporary value 123, which should stay alive until the next statement. But something seems to overwrite it before p is initialized. Can you explain to me what's going on here? How can I fix this code? (And is this the right place to ask?) Thanks in advance Lukas