Title
Should memcpy be allowed into a POD with a const member?
Status
nad
Section
6.8 [basic.types]
Submitter
Garry Lancaster

Created on 2001-06-12.00:00:00 last changed 17 months ago

Messages

Date: 2004-10-15.00:00:00

Rationale (October, 2004):

9.2.9.2 [dcl.type.cv] paragraph 4 already forbids modifying a const member of a POD struct. The prohibition need not be repeated in 6.8 [basic.types].

Date: 2022-11-20.07:54:16

Suggested resolution:

It is recommended that 6.8 [basic.types] paragraph 3 be reworded to exclude PODs which contain (directly or indirectly) members of const-qualified type.

Date: 2022-02-18.07:47:23

Following the definition in Clause 11 [class] paragraph 4 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 7.6.2.8 [expr.new] bullet 15.1, sub-bullet 2 and 11.9.3 [class.base.init] bullet 4.2.

6.8 [basic.types] paragraph 3 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.

[Note: this text was changed by TC1, but the essential point stays the same.]

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.

History
Date User Action Args
2022-11-20 07:54:16adminsetmessages: + msg7054
2004-11-07 00:00:00adminsetmessages: + msg1103
2004-11-07 00:00:00adminsetstatus: open -> nad
2001-06-12 00:00:00admincreate