[Posting second time; first post did not appear after some rounds of
approved messages, even messages submitted later]
<BigMan@[EMAIL PROTECTED]
> schrieb im Newsbeitrag
news:1107339738.256444.178280@[EMAIL PROTECTED]
> 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?
I have to pass this one.
>
> 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;
I don't understand. RW is the type you presented with the copy constructor
taking reference to non-const?
Well, then these 2 lines won't compile, because again you try to bind an
rvalue (CreateRW does return an rvalue, doesn't it?) to reference to
non-const.
BTW, you seem to assume RVO is performed. There is no general guarantee it
will.
> 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;
let the arithmetic operators return proxies that are normally copy-able.
Overload operator+ / - and operator= to take either an RW, or a proxy. The
proxy is, of course, a private class.
> CreateRW( ) = a; // 4: disallow assignment to rvalues and
> tem****aries - it is pointless;
That means CreateRW returns a const-qualified object. If not, this will
still work.
Thomas
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|