C ++ task solution for assigning not really lvalue errors

Given this code:

void FrMemCopy(void *to, const void *from, size_t sz) { size_t sz8 = sz >> 3; size_t sz1 = sz - (sz8 << 3); while (sz8-- != 0) { *((double *)to)++ = *((double *)from)++; } while (sz1-- != 0) { *((char *)to)++ = *((char *)from)++; } } 

I get target of assignment not really an lvalue warnings in two lines inside while loops.

Can anyone break these lines?

and then increment?

What is an easier way to write this?

What does the error mean?

+4
source share
6 answers

This does not like the *((char*)to)++ operator.

Try the following:

 void FrMemCopy(void *to, const void *from, size_t sz) { size_t sz8 = sz >> 3; size_t sz1 = sz - (sz8 << 3); double * tod = (double *) to; double * fromd = (double *) from; while (sz8-- != 0) { *(tod++) = *(fromd++); } char * toc = (char *) tod; char * fromc = (char *) fromd; while (sz1-- != 0) { *(toc++) = *(fromc++); } } 
+4
source

You cannot apply ++ to the result of a cast, only to an lvalue (variable). Therefore, you need to create a new variable with the appropriate types for the increments:

 void FrMemCopy(void *to, const void *from, size_t sz) { size_t sz8 = sz >> 3; size_t sz1 = sz - (sz8 << 3); double *to1 = (double *)to; double *from1 = (double *)from while (sz8-- != 0) { *to1++ = *from1++; } char *to2 = (char *)to1; char *from2 = (char *)from1; while (sz1-- != 0) { *to2++ = *from2++; } } 
+2
source

I tried to rewrite it so that the warning did not appear:

 void FrMemCopy(void *to, const void *from, size_t sz) { size_t sz8 = sz >> 3; size_t sz1 = sz - (sz8 << 3); double *xto = (double *)to; double *xfrom = (double *)from; while (sz8-- != 0) { *xto++ = *xfrom++; } char *cto = (char *)to; char *cfrom = (char *)from; while (sz1-- != 0) { *cto++ = *cfrom++; } } 
+1
source

The result of explicit conversion of rvalue types in this case is according to 5.4.1 of the C ++ 11 standard. You cannot apply the increment operator to rvalue, it must be an lvalue. See the C ++ value category for more details.

Use temporary variables to get the desired effect:

 double* to_dbl = static_cast<double*>(to); double* from_dbl = static_cast<double*>(from); while(sz8-- != 0) { *(to_dbl++) = *(from_dbl++); } 
+1
source

The increment operation is performed by LValue (the value of the left side of the assignment operator). Logically and by definition, LValue should always be a variable. It cannot be permanent. When you perform an increment operation, it leaves a constant value on the left side, which gives you an error.

0
source

Before answering, let me say: do not try to disable the micro-optimization of your compiler / library. Compiler authors will win something like 99 times out of 100. Use std::copy or memcpy depending on the types that you copy and need.

Other answers noted that you can resolve immediate compilation errors with temporary variables.

I do not recommend this under any circumstances do the following, but I believe that you can also accomplish this by casting to a reference type:

 void FrMemCopy(void *to, const void *from, size_t sz) { size_t sz8 = sz >> 3; size_t sz1 = sz - (sz8 << 3); while (sz8-- != 0) { *((double *&)to)++ = *((double *&)from)++; } while (sz1-- != 0) { *((char *&)to)++ = *((char *&)from)++; } } 
0
source

Source: https://habr.com/ru/post/1443049/


All Articles