Created
January 8, 2023 03:20
-
-
Save i-saint/e7366bc760f17e288b1f13c89be58fed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <cstring> | |
#include <utility> | |
template<class T, size_t N> | |
static inline T select_value( | |
std::pair<const char*, T> (&&table)[N], const char* name, T fallback) | |
{ | |
for (auto& kvp : table) { | |
if (std::strcmp(kvp.first, name) == 0) { | |
return kvp.second; | |
} | |
} | |
return fallback; | |
} | |
int main(int argc, char **argv) | |
{ | |
int val = select_value({ | |
{"one", 1}, | |
{"two", 2}, | |
{"tree", 3}, | |
{"four", 4}, | |
}, "tree", 0); | |
printf("%d\n", val); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment