Topic: Defect Report: memcpy to POD with const member


Author: "glancaster" <glancaster@ntlworld.com>
Date: 14 Jun 01 17:03:22 GMT
Raw View
 [Moderator's note: this defect report has been
 forwarded to the C++ committee. -moderator(fjh).]

This is a follow-up to the recent "memcpy to POD with const member" thread,
specifically the later exchanges between myself and Andrei Iltchenko.

Following the definition in 9/4 [class] the following is a valid POD
(actually a POD-struct):

struct test
{
    const int i;
};

The legality of PODs with const members is also implied by the text of 5.3.4
[expr.new] para.15 bullet 1, sub-bullet 2 and 12.6.2 [class.base.init] para.
4 bullet 2

3.9/1 [basic.types] states that "For any POD type T, if two pointers to T
point to distinct objects obj1 and obj2, if the value of obj1 is copied into
obj2, using the memcpy library function, obj2 shall subsequently hold the
same value as obj1."

This implies that the following is required to work:

test obj1 = { 1 };
test obj2 = { 2 };

memcpy( &obj2, &obj1, sizeof(test) );

The memcpy of course changes the value of the const member, surely something
that shouldn't be allowed.

It is recommended that para 3.9/3 be reworded to exclude PODs which contain
(directly or indirectly) members of const-qualified type.

Kind regards

Garry Lancaster
Codemill Ltd
mail << "glancaster" << at << "codemill" << dot << "net";
Visit our web site at http://www.codemill.net
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]