Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of conversion from enumerated values to enumerated names in cantilever 11

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to solve the problem of how to realize the conversion from enumerated values to enumerated names. This article is very detailed and has a certain reference value. Interested friends must finish reading it!

Effect ENUM_DEFINE (Color, Red, Blue,) EnumHelper (Color::Red)-> "Red" EnumHelper (Color::Red, std::toupper)-> "RED" key technology _ _ VA_ARGS__

_ _ VA_ARGS__ implements macros with variable parameters.

# define XXX (type,...) Enum class type {_ _ VA_ARGS__}

XXX (Color, Red, Blue) is equivalent to:

Enum class Color {Red, Blue}; # _ _ VA_ARGS__

# _ _ VA_ARGS__ converts the variable parameters of a macro to a string.

# define XXX (type,...) # _ _ VA_ARGS__

XXX (Color, Red, Blue) is equivalent to "Red, Blue"

The ability to execute code outside a function

Outside the function, you can execute a function by defining a global variable. It is important to note that variable initialization cannot normally be done in the header file unless static or const is added.

Const int temp = initialize ()

In addition, if multiple code files # include the header file, multiple variables will be generated, that is, the temp variables obtained in different code files are not the same. Correspondingly, the initialize function is called multiple times.

Static variables of template functions

The static variable of the function can be used to hold the mapping of enumerated values to enumerated strings, while template functions that use enumerated types as template parameters can directly provide a mapping container for each enumeration.

Key code templatestring EnumHelper (T key, const std::function processor = nullptr, const char* pszName = NULL) {static_assert (std::is_enum_v, _ _ FUNCTION__ "'s key need an enum"); static map smapmapName; if (nullptr! = pszName) {smapNamekey = pszName;} std::string res = ""; auto it = s_mapName.find (key) If (it! = s_mapName.end () res = it- > second; if (nullptr! = processor) std::transform (res.begin (), res.end (), res.begin (), processor); return res;} template size_t analystEnum (T enumClass, const char* pszNames) static_assert (std::is_enum_v, _ FUNCTION__ "'s enumClass need an enum"); cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report