473,320 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Getting around rvalue restriction for temp. created objects

My concrete problem is this (it is a bit special, but there may be other
instances of it):

I'd like to create a class for vectors (say: Vector), exporting math. methods, say v.foo(...).
I'd like to implement 'operator()' s.t. I can treat parts of a vector in the same
way as a normal vector: if v.foo(...) works, so should v(rng).foo(...), where
rng is a range object. This should work even if 'foo' is a non-const method.
I do this by letting Vector objects be normal, or be "masks". Masks refer to normal Vector objects and also maintain a range.

Vector has a copy constructor (CC), which (to ensure a safe handling) ALWAYS
draws a copy of its const Vector& argument, the new object will be normal.
The same safety restriction holds for 'operator=' of Vector. So:

Vector newv(v(rng));
newv=v2(rng2);

newv will be a new normal vector, containing a copy of v(rng), and the
assignment copies v2(rng2) to newv. v(rng) and v2(rng2) are masks, but
newv is always normal.

I could implement 'operator()' having return type 'Vector', returning a mask,
but then s.th. like
v(rng).foo(...);
would lead to a temp. copy of mask -> normal Vector been done, using the CC.
I don't want that for efficiency, but I also do not want to modify the CC to
create masks from masks (would be unsafe in my context).

I tried the following: I created a wrapper class VectorWrap, containing a
member Vector* vec. I added a special CC to Vector:

Vector(const VectorWrap& vw) {
// Take all resources of *vw.vec and make the new vector use them.
// Remove all resources from *vw.vec.
// With "resources", I mean members, such as vector buffer for a normal
// vector, or range for a mask
}

The destructor of VectorWrap destroys *vec, which frees resources of *vec
IF it has any. In my application, it is OK for a Vector object not to have
resources. It is the empty vector then.

Finally:

Vector Vector::operator()(const Range& rng) {
Vecttor* vec;
// Allocate vec as mask to *this with range rng
return VectorWrap(vec);
}

In an expression v(rng), a mask is created and wrapped. The return value
of 'operator()' has different type from its return type, so the special Vector
CC is used, avoiding copying. The temp. VectorWrap object is destroyed,
but this is OK, because at that time, the special Vector CC has already
removed the resources of vec. Fine.

Now my problem: This works fine if v(rng) is used as rvalue in an expression,
say s.th. like w+=v(rng) (if 'operator+=' is given for Vector, with
const Vector& argument.
But it does NOT work if v(rng) is to be used as lvalue. S.th. like

v(rng).foo(...);

is rejected by the compiler. I see why this "rvalue restriction" is in place
in general. v(rng) is temporary, so if 'foo' is not a method declared const,
this should not be allowed in general. The golden rule seems to be: do not
do anything not declared const on a temp. created object.

But in my case, it is safe and makes perfect sense, to allow things like

v(rng)+=w

Add vector w to subvector of v, thereby really modifying v. This is done
via a temp. object, and of course I cannot declare 'operator+=' to be const,
because then

v+=w

would not work.

If you know a simple way of getting around this "rvalue restriction", I'd
be very grateful!

Some "solutions" I considered:
- What I am doing right now: Since I know that vec(rng) should be used as
lvalue, I do not use the special CC above, but:

Vector* VectorWrap::operator->() const {
return vec;
}

VectorWrap::operator Vector&() {
return *vec;
}

Then, vec(rng).foo(...) can at least be done as

vec(rng)->foo(...)

This is awkward, but then I cannot overload '.' of VectorWrap!
The problem here is that the conversion to Vector& is not done
automatically, but has to be forced by casting:

vec(rng)=w;

does not work, but

((Vector&) vec(rng))=w;

does. This is awkward to use.
- I could replicate all public methods of Vector in VectorWrap, but this would
be a really poor solution. Also, there is a mutual dependency problem here
(in my case, all these classes are templates!): Vector needs to know
how to create VectorWrap, so I cannot have VectorWrap know about
definitions of Vector! I did not even manage to implement

VectorWrap::operator=(const Vector& arg) {
...
}

this way, which would maybe get " vec(rng) = w;" working (one would
have to use an abstract base of Vector with methods for '=', to get
around the mutual dependency)

Thanks a lot for help!
Nov 22 '06 #1
0 1351

Sign in to post your reply or Sign up for a free account.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
0
by: Oliver Walczak | last post by:
Can i call normalize to the dom root node so that all adjacent child text nodes attached to one of the element nodes are joined? -----Ursprüngliche Nachricht----- Von:...
2
by: Patrick J. Maloney | last post by:
I received a file from a business partner. I ran it through XercesJ and it choked on the following element: <wcb-case-number>0</wcb-case-number> To debug this issue, I loaded it into XMLSPY...
1
by: Robert McEuen | last post by:
Using Access 97 on WinXP I have data in a DB2 table that I'm trying to get into an identical table in my backend db. Based on volume of data and frequency of download, I'm trying to avoid...
24
by: Romeo Colacitti | last post by:
Hi, Does anyone here have a strong understanding for the meanings of the terms "lvalue" and "rvalue" as it pertains to C, objects, and different contexts? If so please share. I've been...
1
by: Hemant Mohan | last post by:
Consider the following program snipet: <snip> typedef struct { unsigned char a ; unsigned char b ; unsigned char c ;
3
by: Adrian | last post by:
In the following code example I am trying to create a generic interface for a bunch of objects. Each concrete type is stored in its own container and the a container of pointer's to base is used so I...
2
by: Chad | last post by:
The following question actually stems from an old Chris Torek post. And I quote from the following old CLC url ...
9
by: ne0lithic | last post by:
Hello everyone, I was testing a web app on Tomcat and I basically created a 'Temp' folder in the 'Web-content' folder of the project in Eclipse. I write files to that folder in my web app by using...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.