
You are forced to validate it at runtime.
Nothing's really new here. You can't validate XML at compile time. Unless you mean something different.
please see below
Phils suggestion is more typesafe:
No, it's just more verbose, and there's no way to eliminate all the intermediate variables.
Type is enforced only by the structure of the data and the way that it's interpreted by the parser/writer. You may as well pick an interface that just lets you read/write hierarchical data easily, or support an extension that already deals with interpretation, like schemas.
articleinfo ai; { if (title) ai.push_back(comment("This title was moved")); author a; { a.push_back(firstname("Joe")); a.push_back(surname("Random")); ai.push_back(a); } root.push_back(ai); }
I don't really see how it's more type-safe. The structures "articleinfo", "firstname" and "surname" would all inherit from "element", which is presumably a type accepted by "push_back". So nothing prevents you from doing:
root.push_back(firstname("Joe"));
which is exactly what you had hoped to avoid.
If you really want type-safety at this level, you'd have to define a push_back for all the sub-element types accepted by an element (and push_front, insert, etc.). No thanks.
I would do it over templates - than it's type safe and a part of the xml validation can be done at compile time. typedef tag< mpl::vector< title, author > // tags mpl::vector< isbn > // atributes
article_info;
etc. Regards, Oliver