Wednesday 15 April 2015

c++ - Proper method to increment a void* by type size because cast results in temporary, not lvalue -


To upgrade under the modern Xcode 4.x compilers, I have found the following to upgrade the old C ++ code, I have found That the original developer was a programming idiom that used to be avoided but it should be avoided - it is now an error:

  * ((USACHR * PVPxL) ++ = UGG; // Copy Red & amp; Green color (2 bytes)   

I was looking at what was intended: to know how to copy unsigned short (urg) by zero pointer (pvPixel), then by the proper size Increasingly told (in case above, 2 bytes) the problem is that pvPixel casting is in a temporary, not a realway, which is no longer allowed.

The original developer used this idiom in many places - which I would like to rewrite in a short time though, rather than rewriting each context instead of brutal force, providing a beautiful and corrective solution will be good. I can think of several possible options for rewriting the cruel force of each incident: macros, inline functions, maybe a template? My question is: Is there a C ++ syntax / language solution for this problem? What is an approach that would produce clear code for future developers

(two examples work, below):

  zero PrimSurfaceGDI3 :: mFillHLine (uint uRGB, uint UX, UINT UI, UINT UW (LPVOID PVPCell; if (UW> 0) {// surface pvPixel = mPtr (uX, uY) receives an indicator for a specified pixel; USHORT uRG = * (USHORT *) And URBB; BYTE uB = ((BYTE *) and URGB) [2]; LPVOD PVID = (byte *) PVPxL + UW * 3; while (pvPi Xel & lt; pvEnd) {// The two lines are now invalid in modern compilers because casting pvPixel is USHORT * or byte * is a temporary, not the result in lvalue * (USHORT *) pvPixel) ++ = urg; // Copy red & green (2 bytes) * ((BYTE *) pvPixel) ++ = uB; // Copy blue (1 byte)}}   

and below, The requirement for this idiom loop is used in the statement:

  Zero PrimSurfaceGDI3 :: Mphil Veline (UINT URGB, UINT UX, UINT UI, UINT UH) {LPVOA D Pivipiksel = Mpitiar (UX, Uway); USHORT uRG = * (USHORT *) and URBB; BYTE uB = ((byte *) and urgb) [2]; LPVOD PVID = (Byte *) PVPx + UH * MUSCAN; // Required statement is now illegal in modern compilers because casting for a byte is a temporary for pvPixel *, not the result in lvalue (; pvPixel & lt; pvEnd, (byte *) pvPixel) = = muscan) (USHORT *) PvPixel = uRG; // Copy Red & amp; Green (2 bytes) ((byte *) PVPxL) [2] = UB; // Copy blue (1 byte)}}    

To resolve this problem To re-do things again, replace mPtr , then it gives an indicator (or container reference) three byte types in which it has three fields, and then fill it with std: : Use fill_n to input RGB

  Zero PrimSurfaceGDI3 :: mFillHLine (uint uRGB, uint UX, uint UY, uint UW: Then the function is something like this (which is none of your compiler To apply proper customization A shrinking provides a great opportunity)) {if (UW & gt; 0) {color circular Pivipiksl * = Mpitiar (UX, Uway); Std :: fill_n (pvPixel, UW, Color (urgb)); Next} Note that the original "optimization" is likely to cause de-optimization due to all incorrect allocation of two-byte access to the program.   

If you want to make as little as possible changes to the code, then you acknowledge that this code will not work on any architecture other than x86 (due to incorrect alliance access), and you -fno-strict-aliasing (which may be necessary due to casting an unrelated type), then you might have come across a reference: reinterpret_cast :

  * (reinterpret_cast & lt; unsigned short * & amp; (pvPixel)) ++ = uRG;    

No comments:

Post a Comment