From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18245 invoked by alias); 10 Apr 2008 13:32:23 -0000 Received: (qmail 18235 invoked by uid 22791); 10 Apr 2008 13:32:22 -0000 X-Spam-Check-By: sourceware.org Received: from web88305.mail.re4.yahoo.com (HELO web88305.mail.re4.yahoo.com) (216.39.53.228) by sourceware.org (qpsmtpd/0.31) with SMTP; Thu, 10 Apr 2008 13:31:59 +0000 Received: (qmail 60795 invoked by uid 60001); 10 Apr 2008 13:31:57 -0000 X-YMail-OSG: cBEVHJ8VM1lQzfZ5DR5DC45boxf_8vS58HRwyrS8v8ZVLk2dmeq9f_houapzYR6spOej62Aai_FiqVCkpOLwAD.KyjuanV8BDHpoESMYYBA2AJRfV5.DNJrMVOAxPXW723ogGT_ZB24ATg-- Received: from [99.242.109.220] by web88305.mail.re4.yahoo.com via HTTP; Thu, 10 Apr 2008 09:31:57 EDT Date: Thu, 10 Apr 2008 14:45:00 -0000 From: Ted Byers Subject: Re: how to pass params to inline functions by reference or value? To: Vincent Mayeski Cc: gcc-help@gcc.gnu.org In-Reply-To: <1207832200.12695.22.camel@vm-laptop> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Message-ID: <659773.60570.qm@web88305.mail.re4.yahoo.com> X-IsSubscribed: yes 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-04/txt/msg00155.txt.bz2 --- Vincent Mayeski wrote: > Thanks Ted. > > I see your point. I should have included the const > in the first version > of the function. So we would have: > > inline int > sum(const atype_t *x) > { > return x->a + x->b; > } > > and > > inline int > sum(atype_t x) > { > return x.a + x.b; > } > > Now these two functions are semantically identical I > believe. No, not quite. Think about your function that takes pointers. What is constant? The pointer, or the object it points to, or both? In the version that takes the argument by value, what is constant? The address of the object, the object, or both? Now, extend your consideration to assume that the data members 'a' and 'b' are instances of classes that are members of an inheritance tree, and that there are virtual functions involved. Which implementation of these virtual functions SHOULD be called, and which WILL be called? > As you > say, the second implementation suggests that the arg > needs to be copied. > However, IF the function is inlined, no copying > should actually take > place because the arg is not modified within the > function. My guess is > that these will produce identical code. > How is this assumption clear from the body of the function? We don't know what the data members 'a' and 'b' are, or what operator+() does to them. Yes, if they are pods, arg is not modified, but if they are not, almost anything goes? And if you call functions in your inline function, and they call other functions, which in turn call other functions, you can not know that the objects passed as an argument are not modified by the function, unless you have complete and correct specification of const through your entire codebase. And this raises the question of WHICH codebase. The code in question may well be part of a library used in a variety of projects, and the data members accessed in your function may be wrappers for implementing classes, so the wrappers' member functions may well all be inlined, but the implementing classes may or may not guarantee that accessor functions or operators defined for the class guarantee that the object won't be changed. And none of this affects whether or not the function could or should be inlined. I am not sure I'd want a compiler to analyse all that in order to figure out whether or not to copy an argument passed by value when deciding to inline a function. > If the compiler generated different code for each, > then the second > implementation should be faster because there is no > dereferencing > needed. > > Is this correct? > No! Unless someone who has developed the compiler code that handles inlining can tell us that there is a way for the compiler to guarantee that the argument passed by value is not changed without doing the copy, it must be assumed that the copy is done regardless of whether the function is inlined or not. HTH Ted