Can I use an std::map with both keys and values being enum classes in C++11?
c++, c++11, enums, stl
Solution
According to http://en.cppreference.com/w/cpp/container/map std::map is a sorted associative container that contains key-value pairs with unique keys. Therefore you must insert a pair.
Replace
lectureToDayMap.insert(Foobar::Lecture::COMP_SCI, Foobar::Day::MONDAY);
by
mymap.insert(std::make_pair(Foobar::Lecture::COMP_SCI, Foobar::Day::MONDAY));
or better
lectureToDayMap[Foobar::Lecture::COMP_SCI] = Foobar::Day::MONDAY;
Problem
I need a way of mapping C++11 `enum classes` to other `enum classes`. Let's say I have these C++11 style enums: ``` #include <iostream> #include <map> class Foobar { public: enum class Lecture { COMP_SCI, PHYSICS, CHEMISTRY, BIOLOGY, PSYCHOLOGY, MODERN_ART, PHILOSOPHY, SOCIOLOGY }; enum class Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }; }; void test_enum_class_to_enum_class_std_map() { std::map<Foobar::Lecture, Foobar::Day> lectureToDayMap; lectureToDayMap.insert(Foobar::Lecture::COMP_SCI, Foobar::Day::MONDAY); } int main(int argc, char** argv) { test_enum_class_to_enum_class_std_map(); return 0; } ``` Is there a way of mapping from one enum class to another? I want something like `std::map<Foobar::Lecture, Foobar::Day> lectureToDayMap;` If this isn't possible, am I able to convert from an `int` to an `enum class` and vice versa so I can just use a `std::map<int, int>` map and convert to `int` to insert, and back to an `enum class` to retrieve a value? Here is the compilation error When I try the first method: ``` test_cpp11_enums.cpp: In function ‘void test_enum_class_to_enum_class_std_map()’: test_cpp11_enums.cpp:51:74: error: no matching function for call to ‘std::map<Foobar::Lecture, Foobar::Day>::insert(Foobar::Lecture, Foobar::Day)’ lectureToDayMap.insert(Foobar::Lecture::COMP_SCI, Foobar::Day::MONDAY); ^ test_cpp11_enums.cpp:51:74: note: candidates are: In file included from /usr/include/c++/4.8/map:61:0, from test_cpp11_enums.cpp:9: /usr/include/c++/4.8/bits/stl_map.h:594:7: note: std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const value_type&) [with _Key = Foobar::Lecture; _Tp = Foobar::Day; _Compare = std::less<Foobar::Lecture>; _Alloc = std::allocator<std::pair<const Foobar::Lecture, Foobar::Day> >; typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator = std::_Rb_tree_iterator<std::pair<const Foobar::Lecture, Foobar::Day> >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair<const Foobar::Lecture, Foobar::Day>] insert(const value_type& __x) ``` Thank you for your time :) Edit 1: Fixed a missing comma Edit 2: Added the compilation error I get Edit 3: Added the full source of what I am trying out Edit 4: No new code or anything, just an apology to everyone for misunderstanding how these posts work. I have a tough time understanding compilation errors still and I didn't find exactly what I was looking for in Google. I thought it might be easier to ask directly about what I wanted and perhaps get an explanation on how class enums work (or don't) with maps. I will keep playing with my test source and keep looking on Google. Sorry for irritating you all :( Edit 5: You guys were kind enough to show me where my error was, thank you. I didn't see that `sdt::map`'s `insert` function didn't take the key and value as arguments. At first I thought the error meant that no `std::map` named insert exists, and when I checked that it does online, I somehow missed looking at the parameters, becoming convinced this was something to do with `enum classes`, since I never used them before.