![]() There's also a real risk of accidentally omitting one or more of the mappings. If you have large enumerations, you might find that linear search in an array is inefficient. ![]() If (not iss.fail() & 0 (value) // See the comment above. In this section, you will look into all these three methods in detail. Int n = static_cast(EnumIO::enum_names().size()) There are three different ways in which you can perform conversion of an int to string in C++. must be extended by consulting a vector containing members are assigned other integers, this code Here we assume that the integer representation of Should not be used directly.ĮnumRefHolder(T& enumVal): enumVal(enumVal) ) be no definition of a generic version). indicating that there is no definition of this variable (as there should be Any enum that does not have a specialization will generate a compiler error Each enumeration type will declare its own specialization. This is the type that will hold all the strings. I would use some template logic to achieve the affect in a more scalable way: #include Std::string enum_conversion(some_class::enum_type enum_type) Some_class::enum_type enum_conversion(std::string const& enum_type) Type_map = std::make_pair("type2", some_class::enum_type::type2) īool initialization_result = initialize_map() Type_map = std::make_pair("type1", some_class::enum_type::type1) Type_map = std::make_pair("none", some_class::enum_type::none) Token_string = enum_conversion(some_class_instance->get_type()) Īnd to present one enum and related conversions (but there could be more): Some_class_instance->set_type(enum_conversion(token_string)) Random.cpp string token_string = "none" //In reality this will be externally, user, generated. more enums than the one presented here) conversions as implicit as possible The idea would be make the multiple (i.e. This function accepts a number (which can be any data type) and returns the number as the desired string. ![]() Is the following design of separating the conversion functions and putting them to an anonymous namespace good design (I've thought about putting the conversion functions to a file, say conversions.incl and including it)? The tostring () function can be used to convert an integer, floating point values, or any number to a string. convertto/ convertfrom for all the enums, not just per enum type)? Perhaps using some kind deduction magic with auto and decltype? It looks like ambiguity sets in since only the return value can be used to separate the different functions overloads (even if done with function templates). convert) or two different function names (i.e. If there will be more enums, would it be possible to avoid defining two explicit conversion functions per enum type and device a system where the caller sees just one (i.e. The approach doesn't scale if there are more enums and in any event, less-redundant code is also better. I have the following redundant-feeling design to convert between enums and strings regarding a class that stores enums. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |