2 ms·
auto get_type(const std::string& type) { auto e = m.find(type); if (e == std::end(m)) return type_t::num_types; return e->second; }
by user5994461 10y ago
auto get_type(const std::string& type) {
auto e = m.find(type);
if (e == std::end(m)) return type_t::num_types;
return e->second;
}
I don't understand the point of that code. Use "map.at(key)" and you get the value from the map. No need for function and branching and whatever.
If you really want to mess around, you compare "map.at(key)" against "map[key]". at return a const, [] is not const and allows to create the key (if I remember well).
To conclude, if you really really want to show off your optimization skill, you optimize return types: "auto &&" vs "auto &" vs "auto" vs a few other ones.
Can't help you with that last one. One decade of C++ and still struggling with reference, value copy, left-value reference...
That reminds me how bad C++ is a mess. 5 minutes of optimizations and my brain is already hurting. Good thing I moved to DevOps. More pay, less hassle.
- charles-salvia 10y agoThe point is apparently to return "type_t::num_types" if the key doesn't exist in the map. Using at() throws an exception if the key doesn't exist.
- user5994461 10y agoThat's an artificial requirements, you can do the same with at() or change the requirements. Catching all errors to return a default type is an arguable decision.