auto
Use case
Simplify iterator-heavy code when walking symbol tables.
Explanation
auto lets the compiler deduce types. Reduces verbosity with STL containers and iterators.
Code
#include <cstdint>
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<uint64_t, std::string> symbols = {{0x10001000, "_main"},
{0x10002000, "_helper"}};
auto findSymbol = [&](uint64_t addr) -> std::string {
// std::__1::map<uint64_t, std::__1::string>::iterator it
auto it = symbols.find(addr);
return (it != symbols.end()) ? it->second : "<unknown>";
};
std::cout << "Lookup 0x10001000: " << findSymbol(0x10001000) << "\n";
std::cout << "Lookup 0x20000000: " << findSymbol(0x20000000) << "\n";
return 0;
}
Output
$ ./src/c++11/build/auto
Lookup 0x10001000: _main
Lookup 0x20000000: <unknown>