serialization - C++ class "upgrade" with minimal boiler-plate -



serialization - C++ class "upgrade" with minimal boiler-plate -

let's have following:

struct c1 { int w; } struct b1 { int x; int y; int z; c1 c; }; struct a1 { int x; int y; int z; b1 b; }; struct c2 { long w; short t; } struct b2 { int x; int y; int z; c2 c; }; struct a2 { int x; int y; int z; b2 b; };

and want implement function

a2 upgrade(const a1 &a1);

such takes a1 object, , creates a2 object (assume a2.w = a1.w , a2.t = 6).

an ideal (but impossible knowledge) implementation following:

a2 upgrade(const a1 &a1) { dynamic d = to_dyn(a1); d["b"]["c"]["w"] = (long)d["b"]["c"]["w"]; d["b"]["c"]["z"] = (short)5; a2 a2 = from_dyn(d); homecoming a2; }

how close can in actual c++?

as classes aggregates, concise , straight-forward using aggregate-initialization:

a2 upgrade(const a1& a) { homecoming {a.x, a.y, a.z, {a.b.x, a.b.y, a.b.z, {a.b.c.w, (short)5}}}; }

c++ not have facility iterate on types members, neither compile-time nor runtime. there people working on proposal add together such compile-time facility language, still needs much work.

c++ serialization dynamic reflection types

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -