mardi 22 décembre 2015

Alternative to switch(name_of_type) and maps when printing data?

Suppose I have a function with the following signature:

void print_elements(void* b, const std::string element_type_name, size_t len, std::ostream& os);

Which is supposed to print/stream the len elements of b to output stream os. Obviously I can't do

const size_t element_size = size_by_type_name(element_type_name);
for(it = b; it < (b + len * element_size); it++) {
    auto& stream_op_wrapper = get_stream_op_wrapper_for(os, element_type_name);
    stream_op(os, it);
}

in C++. What do I do instead? I know C++ doesn't have reflection, but can I do better than looking up each time in a table of types? Or using static maps of string-to-typpe_info's ?





Aucun commentaire:

Enregistrer un commentaire