c++ - Incomplete types and initializer_list -
i trying model meta data serializing/de-serializing c++ objects. here captures nuts & bolts of need; compiles gcc 5.2 (g++ sample.cpp -std=c++14
) , clang 3.6 (clang++ sample.cpp -std=c++14
).
my question struct typeinfo
in example. contains std::initializer_list
of itself. standards-conforming?
#include <cstdint> #include <initializer_list> enum class typecode : std::uint8_t { boolean, int, object, string, sentinel }; struct typeinfo { typecode typecode_; char fieldname_[64]; union { std::uint16_t textminlength_; std::uint16_t objectversionmajor_; }; union { std::uint16_t textmaxlength_; std::uint16_t objectversionminor_; }; // set if typecode_ = object std::initializer_list < typeinfo > objecttypeinfos_; }; int main() { typeinfo const sti { typecode::string, "updatedby", { .textminlength_ = 0 }, { .textmaxlength_ = 16 } }; typeinfo const iti { typecode::int, "amount", { 0 }, { 0 } }; typeinfo const oti { typecode::object, "starttime", { .objectversionmajor_ = 1 }, { .objectversionminor_ = 0 }, { typeinfo { typecode::int, "weekdays", { 0 }, { 0 } }, typeinfo { typecode::int, "timeofday", { 0 }, { 0 } }, typeinfo { typecode::string, "timezone", { .textminlength_ = 0 }, { .textmaxlength_ = 5 } } } }; typeinfo const noti { typecode::object, "schedule", { .objectversionmajor_ = 1 }, { .objectversionminor_ = 0 }, { typeinfo { typecode::int, "id", { 0 }, { 0 } }, typeinfo { typecode::string, "description", { .textminlength_ = 0 }, { .textmaxlength_ = 16 } }, typeinfo { typecode::object, "starttime", { .objectversionmajor_ = 1 }, { .objectversionminor_ = 0 }, { typeinfo { typecode::int, "weekdays", { 0 }, { 0 } }, typeinfo { typecode::int, "timeofday", { 0 }, { 0 } }, typeinfo { typecode::string, "timezone", { .textminlength_ = 0 }, { .textmaxlength_ = 5 } } } } } }; }
that induces undefined behavior current wording. @ point of instantiation of std::initializer_list<typeinfo>
, typeinfo
incomplete, hence [res.on.functions]/(2.5) applies:
in particular, effects undefined in following cases:
(2.5) — if incomplete type (3.9) used template argument when instantiating template component, unless allowed component.
… , incomplete types not allowed initializer_list
yet - however, that's defective. lwg issue 2493 opts fix this:
the typical use-case of
std::initializer_list<t>
pass-by-value parameter oft
's constructor. however, contravenes [res.on.functions]/2.5 because initializer_list doesn't allow incomplete types (as examplestd::unique_ptr
([unique.ptr]/5) ,std::enable_shared_from_this
([util.smartptr.enab]/2)).a resolution copy-paste relevant text such paragraph.
i.e. code fine (and officially fine after resolution of aforementioned dr).
Comments
Post a Comment