Skip to content

Instantly share code, notes, and snippets.

@DieHertz
Last active August 29, 2015 13:56
Show Gist options
  • Save DieHertz/8971454 to your computer and use it in GitHub Desktop.
Save DieHertz/8971454 to your computer and use it in GitHub Desktop.
#include <unordered_map>
#include <utility>
#include <iostream>
using namespace std;
template<class T1, class T2> void fill(unordered_map<T1, T2>& m) {}
template<class T1, class T2, class Key, class Value, class... Args>
void fill(unordered_map<T1, T2>& m, Key&& key, Value&& value, Args&&... args) {
m.insert({ forward<Key>(key), forward<Value>(value) });
fill(m, forward<Args>(args)...);
}
template<class T1, class T2, class... Args>
unordered_map<T1, T2> unordered_map_init(Args&&... args) {
auto m = unordered_map<T1, T2>{};
fill(m, forward<Args>(args)...);
return m;
}
int main() {
static auto m = unordered_map_init<int, int>(1, 2, 3, 4);
for (auto& p : m) cout << p.first << " => " << p.second << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment