Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Leda > Re: Calling ope...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 7 of 8 Topic 188 of 212
Post > Topic >>

Re: Calling operator + multiple times in the same expression.

by kanze@[EMAIL PROTECTED] Feb 3, 2005 at 10:19 AM

BigMan@[EMAIL PROTECTED]
 wrote:
> I see, the problem seems to come from 8.5.3/5...

> 1. I'm curious about why the compiler should want to make a
> copy of an rvalue and bind a const reference to it instead of
> binding the const reference directly to the rvalue. Can anyone
> explain this?

The compiler doesn't want to.  For some reason, the standard
says that it has to be able to, however.

> 2. Why I need a copy ctor taking a non-const reference?
> Because I'm trying to create a type (e.g. class) that
> describes a resource wrapper. The resource is a non-copyable
> one (it can be duplicated, but one should avoid doing so for
> performance reasons). Examples of such resources are: Windows
> HANDLE's, HKEY's, HMODULE's, HINTERNET's, MPI communicators
> and datatypes, and last but not least - huge amounts of
> dynamically allocated memory. I need to use the type that
> describes the resource wrapper (let's name it RW) in
> expressions such as:

> RW a = CreateRW( );			// 1: create a wrapper around a mutable
> resource;
> RW const a = CreateRW( );	// 2: create a wrapper around a
> non-mutable resource;
> a = b + c + d;				// 3: use wrappers in arithmetic expressions, just
> like built-in types; all identifiers here stand for objects of type
RW
> or const RW;
> CreateRW( ) = a;				// 4: disallow assignment to rvalues and
> tem****aries - it is pointless;

> Is it possible to create a type (or two - one for const
> wrappers, and one for non-const ones) that meets all 4
> requirements? If so, how?

The usual solution is to use some sort of reference counting,
with the visible class acting as a handle for the resource, and
forwarding to it (a bit like in the letter/envelope idiom).  The
wrapper class has full copy semantics, but the actual copy is
only shallow.  (Depending on the desired semantics, you might
implement copy on write, or you might simply do***ent the
reference semantics.)

--
James Kanze           GABI Software         http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 8 Posts in Topic:
Calling operator + multiple times in the same expression.
BigMan@[EMAIL PROTECTED]   2005-02-01 15:09:17 
Re: Calling operator + multiple times in the same expression.
"Abhishek Pamecha&qu  2005-02-01 18:23:20 
Re: Calling operator + multiple times in the same expression.
Victor Bazarov <v.Abaz  2005-02-01 18:28:35 
Re: Calling operator + multiple times in the same expression.
"Thomas Mang" &  2005-02-01 18:27:29 
Re: Calling operator + multiple times in the same expression.
BigMan@[EMAIL PROTECTED]   2005-02-02 15:02:26 
Re: Calling operator + multiple times in the same expression.
"Thomas Tutone"  2005-02-03 05:41:22 
Re: Calling operator + multiple times in the same expression.
kanze@[EMAIL PROTECTED]   2005-02-03 10:19:34 
Re: Calling operator + multiple times in the same expression.
"Thomas Mang" &  2005-02-05 20:44:40 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 2:44:59 CDT 2008.