I would like to make my app’s std::maps’ keys not be ints, but rather be more strongly typed as templated non-type enums defined as a member of a struct. The first program below shows the concept of how my app currently uses maps. It compiles and runs ok.
#include <map>
template< int >
struct NummedMap
{
typedef std::map< int, NummedMap > NumableMap;
NummedMap() {}
NumableMap numableMap;
};
int main()
{
NummedMap< 3 > numableMap3;
NummedMap< 4 > numableMap4;
numableMap3.numableMap[ 3 ] = numableMap3;
numableMap4.numableMap[ 4 ] = numableMap4;
return 0;
}
The second program shows how I would like to program my app’s maps, but I am missing some concepts regarding non-type templates and why an < enum EnumT > is different from a POD int.
#include <map>
struct Enums1 // a struct containing one kind scoped enums
{
enum class Action
{
AAAA,
BBBB
};
};
struct Enums2 // a struct containing another kind scoped enums
{
enum class Action
{
CCCC,
DDDD
};
};
template< enum EnumT >
struct EnummedMap // a struct containing a map whose key is non-type templateable
{
typedef std::map< EnumT, EnummedMap > EnumableMap; // error C2065: 'EnumT': undeclared identifier
EnummedMap() {}
EnumableMap enumableMap;
};
int main()
{
EnummedMap< Enums1::Action > enummedMap1; // error C2993: illegal type for non-type template parameter
EnummedMap< Enums2::Action > enummedMap2; // error C2993: illegal type for non-type template parameter
enummedMap1.enumableMap[ Enums1::Action::AAAA ] = enummedMap1; // error C2678: binary '[': no operator found which takes a left-hand operand of type
enummedMap2.enumableMap[ Enums2::Action::CCCC ] = enummedMap2; // error C2678: binary '[': no operator found which takes a left-hand operand of type
return 0;
}
I don’t understand why EnumableMap‘s key is undeclared, or why for example Enums1::Action does not function roughly like an int key.
be more strongly typedin a large program translates into greater program clarity, better error checking and in general easier code maintenance.