解析枚举类变量名称字符串

发布于 2025-02-04 04:22:20 字数 1020 浏览 2 评论 0 原文

考虑一下,我有以下 enum 类:

enum class TestEnum
{
   None = 0,
   Foo,
   Bar
};

我想指定 ostream operator(<<<)因此,我可以写:

std::cout << "This is " << TestEnum::Foo;

并获取以下输出这是foo

我的问题是:
是否有枚举“ name Specifier ”存储的地方? (即枚举类 testEnum,它是 none foo and bar ))指定 Ostream 操作员的函数(或最多函数模板)的 testEnum 喜欢:

std::ostream& operator<< ( std::ostream& os, TestEnum aEnum ) {
   return std::string( aEnum.name() );
}

到目前为止,我这样做了:

std::ostream& operator<< ( std::ostream& os, TestEnum aEnum ) {
   switch( aEnum )
   {
   case TestEnum::Foo:
       os << "Foo";
       break;
   case TestEnum::Bar:
       os << "Bar"
       break;
   }
   return os;
}

我已经使用看到了一些解决方案Boost 库,但我希望这次不使用它。

Consider, I have got the following enum class:

enum class TestEnum
{
   None = 0,
   Foo,
   Bar
};

I'd like to specify ostream operator ( << ) for this enum class, so I could write:

std::cout << "This is " << TestEnum::Foo;

and get following output This is Foo.

My question is:
Is there any place where enum "name specifiers" are stored? (i.e. for enum class TestEnum it is None, Foo and Bar) So I could write a function (or at best function template) that specifies ostream operator for this TestEnum like:

std::ostream& operator<< ( std::ostream& os, TestEnum aEnum ) {
   return std::string( aEnum.name() );
}

So far, I did it this way:

std::ostream& operator<< ( std::ostream& os, TestEnum aEnum ) {
   switch( aEnum )
   {
   case TestEnum::Foo:
       os << "Foo";
       break;
   case TestEnum::Bar:
       os << "Bar"
       break;
   }
   return os;
}

I have seen some solutions using boost library, but I would prefer not using it this time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喜爱纠缠 2025-02-11 04:22:20

是否有存储枚举“名称说明器”的地方?

不,但是一个选项是使用 std :: map&lt; testEnum,std :: string&gt; 如下所示:

enum class TestEnum
{
   None = 0,
   Foo,
   Bar
};
const std::map<TestEnum,std::string> myMap{{TestEnum::None, "None"}, 
                                     {TestEnum::Foo, "Foo"}, 
                                     {TestEnum::Bar, "Bar"}};
    
std::ostream& operator<< ( std::ostream& os, TestEnum aEnum )
{
   os << myMap.at(aEnum);
   return os;
}
int main()
{
    std::cout << "This is " << TestEnum::Foo; //prints This is Foo 
    std::cout << "This is " << TestEnum::Bar; //prints This is Bar

    return 0;
}

demo

Is there any place where enum "name specifiers" are stored?

No, but one option is to use std::map<TestEnum, std::string> as shown below:

enum class TestEnum
{
   None = 0,
   Foo,
   Bar
};
const std::map<TestEnum,std::string> myMap{{TestEnum::None, "None"}, 
                                     {TestEnum::Foo, "Foo"}, 
                                     {TestEnum::Bar, "Bar"}};
    
std::ostream& operator<< ( std::ostream& os, TestEnum aEnum )
{
   os << myMap.at(aEnum);
   return os;
}
int main()
{
    std::cout << "This is " << TestEnum::Foo; //prints This is Foo 
    std::cout << "This is " << TestEnum::Bar; //prints This is Bar

    return 0;
}

Demo

思慕 2025-02-11 04:22:20

存储“ name specifier”的地方吗?


不,名称在任何地方都没有保存。如果需要,需要(不幸的是)对它们进行映射。

如果 enum 值可以用于数组索引(例如OP的情况),则可以使用 std :: string_view 的数组进行映射(必需 c ++ 17 或以后,否则,否则 std :: String s)。

数组的使用使以下解决方案轻巧和O(1)查找。

#include <iostream>
#include <string_view>
#include <type_traits> // std::underlying_type_t
using namespace std::string_view_literals;

enum struct TestEnum { None = 0,   Foo,   Bar };
inline static constexpr std::string_view enumArray[]{"None"sv, "Foo"sv, "Bar"sv};

std::ostream& operator<< (std::ostream& os, TestEnum aEnum)
{
    return os << enumArray[static_cast<std::underlying_type_t<TestEnum>>(aEnum)];
}

int main()
{
    std::cout << "This is " << TestEnum::Foo; // prints: "This is Foo" 
    std::cout << "This is " << TestEnum::Bar; // prints: "This is Bar"
}

请参阅演示

Is there any place where enum "name specifiers" are stored?

No, the names are not saved anywhere. If required, one needs to (unfortunately) make a mapping of them.

If the enum values can be used for array indexing (like OP's case) one might can make a mapping using array of std::string_view(required or later, otherwise array of std::strings).

The usage of array, makes the following solution lightweight and O(1) look-up.

#include <iostream>
#include <string_view>
#include <type_traits> // std::underlying_type_t
using namespace std::string_view_literals;

enum struct TestEnum { None = 0,   Foo,   Bar };
inline static constexpr std::string_view enumArray[]{"None"sv, "Foo"sv, "Bar"sv};

std::ostream& operator<< (std::ostream& os, TestEnum aEnum)
{
    return os << enumArray[static_cast<std::underlying_type_t<TestEnum>>(aEnum)];
}

int main()
{
    std::cout << "This is " << TestEnum::Foo; // prints: "This is Foo" 
    std::cout << "This is " << TestEnum::Bar; // prints: "This is Bar"
}

See a demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文