From 313990556319683544
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,f5d433881cc29cff
X-Google-Attributes: gidf78e5,public
From: Pete Becker <petebecker@acm.org>
Subject: Re: Named return value optimisation- reprise.
Date: 1997/06/17
Message-ID: <33A6A1F9.D0E@acm.org>#1/1
X-Deja-AN: 249094790
References: <33a345bf.7425526@news.ipswich.gil.com.au> <33A478C1.329@acm.org> <33a4e2d5.24720499@news.ipswich.gil.com.au> <33A54D26.F46@acm.org> <5o3v6c$cv3@mulga.cs.mu.OZ.AU> <33A5A723.E9F@acm.org> <5o5mgd$mkv@mulga.cs.mu.OZ.AU>
X-Original-Date: Tue, 17 Jun 1997 10:40:57 -0400
Organization: Pete Becker Consulting
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBM6a6gEy4NqrwXLNJAQHX0wIAxnTmdykgLtHp8k1n5xjUiXKQl9Tovmtz t48OqWWixekC4DO8o6ZL/JlZNVfI5w6H2YZdSQ/7W7vDCrWIa28K+w== =NTvI
Reply-To: petebecker@acm.org
Newsgroups: comp.std.c++
Originator: austern@isolde.mti.sgi.com


Fergus Henderson wrote:
> 
> Pete Becker <petebecker@acm.org> writes:
> 
>  >Fergus Henderson wrote:
>  > >
>  > > Could you give us an example where a compiler could take advantage
>  > > of this rule (12.8[class.copy]/15), assuming your interpretation,
>  > > but where the compiler couldn't already perform the same optimization
>  > > by the "as-if" rule?
>  >
>  >#include <iostream>
>  >
>  >class C {
>  >public:
>  >      static int destructor_count;
>  >      ~C() { destructor_count++; }
>  >};
>  >
>  >int C::destructor_count = 0;
>  >
>  >void f( C c ) { }
>  >
>  >int main() {
>  >      C c;
>  >      f(c);
>  >      if( C::destructor_count != 0 )
>  >              cout << "Already got a destructor call\n";
>  >      return 0;
>  >}
> 
> But in this example, `C::destructor_count' is conceptually part of all
> C objects.  Thus the reference to `C::destructor_count != 0' would
> count as a use of both the `c' in main() and the `c' in f().  Hence
> 12.8/15 doesn't apply, because the objects in question are not unused.
> 
> OK, so where did I go wrong?
> 

	You know, after I posted this I realized it was too clever. Making
destructor_count a global variable makes it clearer. I'm pretty sure
that I think static data is not part of an object, but that's an
unnecessary complication here.
	The real point of all this is this:

class C1
{
public:
	C() {}
	void set_data( int i ) { data[0] = i; }
	int get_data() const { return data[0]; }
private:
	int data[1];
};

class C2
{
public:
	C() : data(new int) {}
	// suitable assignment operator and copy constructor, of course...
	~C2() { delete data; }

	void set_data( int i ) { data[0] = i; }
	int get_data() const { return data[0]; }
private:
	int *data;
};

	I don't see any fundamental difference between these two classes,
merely a difference in implementation. A definition of "object" that
says they have different properties is wrong. Internally they are
somewhat different, but from the outside they present exactly the same
interface and exactly the same states and behaviors.
	-- Pete
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]



