P99

◆ P99_LCOPY

#define P99_LCOPY (   TYPE,
  VAR,
  ... 
)    ((TYPE)P99_DESIGNATED(VAR, __VA_ARGS__))

Construct a compound literal of type TYPE by copying fields of VAR.

The argument list must be composed of designators, something like

struct toto { int a; int b; } A = { .a = 9, .b = 7 };
struct toto B = P99_LCOPY(struct toto, A, .b);
double C[4] = { 1, 2, 3, 4 };
double *D = P99_LCOPY(double[4], C, [0], [2]);

So here B.b is initialized with the value of A.b and B.a, since .a is not in the list, is initialized by 0. Likewise D gets copies of C[0] and C[2] and the other fields are initialized to 0.

This is probably best used indirectly inside a type specific macro. E.g if you know that the copy operation for toto as above should always copy only .b and not .a you could do

#define TOTO_LCOPY(A) P99_LCOPY(struct toto, A, .b);
struct toto B = TOTO_LCOPY(A);
See also
P99_DESIGNATED
Warning
argument 1 may be evaluated multiple times
Remarks
argument 0 should correspond to a type that is not a VLA.
argument 2 must be a field designator
argument 3 must be a field designator
argument 4 must be a field designator

Definition at line 299 of file p99_map.h.

P99_LCOPY
#define P99_LCOPY(TYPE, VAR,...)
Construct a compound literal of type TYPE by copying fields of VAR.
Definition: p99_map.h:299