Я был очень удивлен, что приведенный ниже типы типов «res = 0», скомпилируемым с оптимизацией как на MSVC (код), так и в Clang (The Code), в то время как он висит (как и ожидалось) без оптимизации. < /p>
#include
#include
#include
#include
const char* type_to_text(std::size_t type)
{
return std::array{
"one",
"two",
"three",
""
} [(int)type];
}
std::size_t text_to_type(const char* name) {
std::size_t type = 0;
const char* name_of_type = nullptr;
do {
name_of_type = type_to_text(type);
if (strcmp(name_of_type, name) == 0) {
return type;
}
// ++type; // 0);
return ((std::size_t)-1);
}
int main()
{
std::string name("two");
auto res = text_to_type(name.c_str());
std::cout
Подробнее здесь: https://stackoverflow.com/questions/795 ... rn-a-value