c++ - What are std::initializer_list constructors used for, except for filling some container with values? -
one main , obvious meaning, found in standard library - "initializing collection list of elements":
std::vector<int> v = {1, 2, 3};
another meaning can found behind link on std::bitset below - "the single value assembled elements of initializer_list".
the third example in standard library std::piecewise_constant_distribution, hesitate semantic has, not collection of elements.
what other use cases std::initializer_list constructors? if possible, examples real code.
it's question class design.
because of pecularities of list-initialization adding std::initializer_list constructor existing class can easily surprisingly breaking change, when writing new class should know in advance if ever need std::initializer_list constructor.
so, i'm trying simulate ability see future writing out use cases std::initializer_list constructors.
the primary question is: how determine class have (not surprising users) std::initializer_list constructors in future write correct non-std::initializer_list constructors now?
i using technique in current project. have class "basemenu" , needs std::vector initialized in it's constructor, has std::vector it's argument. then, have mainmenu inherits basemenu , tell basemenu want mainmenu like:
basemenu(std::vector<std::string>); mainmenu::mainmenu(): basemenu({{"play"}, {"options"}, {"about"}, {"quit"}})
it "comfortable". ps: model above simplified, should give sense of usefulness of std::initializer_list
Comments
Post a Comment