上記の @MarmouCorp ではなく、明らかにhttp://www.codeguru.com/cpp/cpp/cpp_mfc/article.php/c4067/Switch-on-Strings-in-C.htmの C++ 11 更新
2 つのマップを使用して、文字列とクラス列挙型の間で変換します (その値はその内部でスコープされているため、単純な列挙型よりも優れており、適切なエラー メッセージの逆ルックアップが行われます)。
codeguru コードでの static の使用は、VS 2013 plus を意味する初期化子リストのコンパイラ サポートにより可能です。gcc 4.8.1 は問題ありませんでしたが、どれくらい前に互換性があるかはわかりません。
/// <summary>
/// Enum for String values we want to switch on
/// </summary>
enum class TestType
{
SetType,
GetType
};
/// <summary>
/// Map from strings to enum values
/// </summary>
std::map<std::string, TestType> MnCTest::s_mapStringToTestType =
{
{ "setType", TestType::SetType },
{ "getType", TestType::GetType }
};
/// <summary>
/// Map from enum values to strings
/// </summary>
std::map<TestType, std::string> MnCTest::s_mapTestTypeToString
{
{TestType::SetType, "setType"},
{TestType::GetType, "getType"},
};
...
std::string someString = "setType";
TestType testType = s_mapStringToTestType[someString];
switch (testType)
{
case TestType::SetType:
break;
case TestType::GetType:
break;
default:
LogError("Unknown TestType ", s_mapTestTypeToString[testType]);
}