Skip to content

Instantly share code, notes, and snippets.

@i-saint
Created January 8, 2023 03:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save i-saint/e7366bc760f17e288b1f13c89be58fed to your computer and use it in GitHub Desktop.
Save i-saint/e7366bc760f17e288b1f13c89be58fed to your computer and use it in GitHub Desktop.
#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