久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在現(xiàn)代 C++11/C++14/C++17 和未來的 C++20 中枚舉到字符

enum to string in modern C++11 / C++14 / C++17 and future C++20(在現(xiàn)代 C++11/C++14/C++17 和未來的 C++20 中枚舉到字符串)
本文介紹了在現(xiàn)代 C++11/C++14/C++17 和未來的 C++20 中枚舉到字符串的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

與所有其他類似問題相反,這個問題是關于使用新的 C++ 特性.

  • 2008 c 有沒有一種簡單的方法可以將 C++ 枚舉轉(zhuǎn)換為字符串?
  • 2008 c 在 C 中使用枚舉類型的變量作為字符串的簡單方法?
  • 2008 c++ 如何輕松地將 C++ 枚舉映射到字符串
  • 2008 c++ 使某些東西既是 C 標識符又是字符串?
  • 2008 c++ 是否有將 C++ 枚舉轉(zhuǎn)換為字符串的簡單腳本?
  • 2009 c++ 如何在 C++ 中使用枚舉作為標志?
  • 2011 c++ 如何將枚舉類型變量轉(zhuǎn)換為字符串?
  • 2011 c++ 枚舉到字符串 C++
  • 2011 c++ 如何將枚舉類型變量轉(zhuǎn)換為字符串?
  • 2012 c 如何在 c 中將枚舉名稱轉(zhuǎn)換為字符串
  • 2013 c 在 C 中對條件編譯的枚舉進行字符串化

看了很多答案,我還沒有找到:

  • 使用 C++11 的優(yōu)雅方式,C++14 或 C++17 新功能
  • 或者在 Boost
  • 中準備使用的東西
  • 其他為 C++20
  • 的計劃

示例

一個例子往往比一個長的解釋要好.
您可以在 Coliru 上編譯并運行此代碼段.
(另一個前面的例子也是可用的)

#include #include 結構體{枚舉類 MyEnum : char {AAA = -8,BBB = '8',CCC = AAA + BBB};};//用一些更快的編譯時生成的代碼替換magic()//(你可以用 std::string 替換返回類型//如果這對你來說更容易)const char* 魔法 (MyClass::MyEnum e){const std::map我的枚舉字符串{{ MyClass::MyEnum::AAA, "MyClass::MyEnum::AAA";},{ MyClass::MyEnum::BBB, "MyClass::MyEnum::BBB";},{ MyClass::MyEnum::CCC, "MyClass::MyEnum::CCC";}};auto it = MyEnumStrings.find(e);返回它 == MyEnumStrings.end() ?超出范圍":它->第二;}int main(){std::cout <<魔術(MyClass::MyEnum::AAA)<<'
';std::cout <<魔術(MyClass::MyEnum::BBB)<<'
';std::cout <<魔術(MyClass::MyEnum::CCC)<<'
';}

限制條件

  • 請不要重復其他答案或基本鏈接.莉>
  • 請避免基于宏的臃腫答案,或盡量減少 #define 開銷.
  • 請不要手動enum ->string 映射.

很高興有

  • 支持 enum 值從非零的數(shù)字開始
  • 支持負enum
  • 支持碎片化的enum
  • 支持類枚舉 (C++11)
  • 支持 class enum : 具有任何允許的 (C++11)
  • 編譯時(非運行時)轉(zhuǎn)換為字符串,
    或者至少在運行時快速執(zhí)行(例如 std::map 不是一個好主意......)
  • constexpr(C++11,然后在 C++14/17/20 中放松)
  • noexcept (C++11)
  • C++17/C++20 友好代碼段

一個可能的想法是使用 C++ 編譯器功能在編譯時使用基于 variadic template classconstexpr 函數(shù)的元編程技巧生成 C++ 代碼..

解決方案

Magic Enum 僅標頭庫為 C++17 的枚舉(到字符串、從字符串、迭代)提供靜態(tài)反射.

#include 枚舉顏色 { 紅色 = 2, 藍色 = 4, 綠色 = 8 };顏色顏色 = 顏色::紅色;auto color_name = magic_enum::enum_name(color);//顏色名稱 ->紅色的"std::string color_name{"GREEN"};自動顏色 = magic_enum::enum_cast(color_name)如果(顏色.has_value()){//color.value() ->顏色::綠色};

有關更多示例,請查看主存儲庫 https://github.com/Neargye/magic_enum.>

缺點在哪里?

此庫使用特定于編譯器的 hack(基于 __PRETTY_FUNCTION__/__FUNCSIG__),適用于 Clang >= 5、MSVC >= 15.3 和 GCC >= 9.

枚舉值必須在 [MAGIC_ENUM_RANGE_MIN, MAGIC_ENUM_RANGE_MAX] 范圍內(nèi).

  • 默認MAGIC_ENUM_RANGE_MIN = -128MAGIC_ENUM_RANGE_MAX = 128.

  • 如果所有枚舉類型默認需要另一個范圍,重新定義宏MAGIC_ENUM_RANGE_MINMAGIC_ENUM_RANGE_MAX.

  • MAGIC_ENUM_RANGE_MIN 必須小于或等于 0,并且必須大于 INT16_MIN.

  • MAGIC_ENUM_RANGE_MAX 必須大于 0 且必須小于 INT16_MAX.

  • 如果需要特定枚舉類型的另一個范圍,請為必要的枚舉類型添加特殊化 enum_range.

    #include 枚舉數(shù) { 一 = 100, 二 = 200, 三 = 300 };命名空間magic_enum {模板 <>struct enum_range{靜態(tài) constexpr int min = 100;靜態(tài) constexpr int 最大值 = 300;};}

Contrary to all other similar questions, this question is about using the new C++ features.

  • 2008 c Is there a simple way to convert C++ enum to string?
  • 2008 c Easy way to use variables of enum types as string in C?
  • 2008 c++ How to easily map c++ enums to strings
  • 2008 c++ Making something both a C identifier and a string?
  • 2008 c++ Is there a simple script to convert C++ enum to string?
  • 2009 c++ How to use enums as flags in C++?
  • 2011 c++ How to convert an enum type variable to a string?
  • 2011 c++ Enum to String C++
  • 2011 c++ How to convert an enum type variable to a string?
  • 2012 c How to convert enum names to string in c
  • 2013 c Stringifying an conditionally compiled enum in C

After reading many answers, I did not yet find any:

  • Elegant way using C++11, C++14 or C++17 new features
  • Or something ready-to-use in Boost
  • Else something planned for C++20

Example

An example is often better than a long explanation.
You can compile and run this snippet on Coliru.
(Another former example is also available)

#include <map>
#include <iostream>

struct MyClass
{
    enum class MyEnum : char {
        AAA = -8,
        BBB = '8',
        CCC = AAA + BBB
    };
};

// Replace magic() by some faster compile-time generated code
// (you're allowed to replace the return type with std::string
// if that's easier for you)
const char* magic (MyClass::MyEnum e)
{
    const std::map<MyClass::MyEnum,const char*> MyEnumStrings {
        { MyClass::MyEnum::AAA, "MyClass::MyEnum::AAA" },
        { MyClass::MyEnum::BBB, "MyClass::MyEnum::BBB" },
        { MyClass::MyEnum::CCC, "MyClass::MyEnum::CCC" }
    };
    auto   it  = MyEnumStrings.find(e);
    return it == MyEnumStrings.end() ? "Out of range" : it->second;
}

int main()
{
   std::cout << magic(MyClass::MyEnum::AAA) <<'
';
   std::cout << magic(MyClass::MyEnum::BBB) <<'
';
   std::cout << magic(MyClass::MyEnum::CCC) <<'
';
}

Constraints

  • Please no valueless duplication of other answers or basic link.
  • Please avoid bloat macro-based answer, or try to reduce the #define overhead as minimum as possible.
  • Please no manual enum -> string mapping.

Nice to have

  • Support enum values starting from a number different from zero
  • Support negative enum values
  • Support fragmented enum values
  • Support class enum (C++11)
  • Support class enum : <type> having any allowed <type> (C++11)
  • Compile-time (not run-time) conversions to a string,
    or at least fast execution at run-time (e.g. std::map is not a great idea...)
  • constexpr (C++11, then relaxed in C++14/17/20)
  • noexcept (C++11)
  • C++17/C++20 friendly snippet

One possible idea could be using the C++ compiler capabilities to generate C++ code at compilation-time using meta-programming tricks based on variadic template class and constexpr functions...

解決方案

Magic Enum header-only library provides static reflection for enums (to string, from string, iteration) for C++17.

#include <magic_enum.hpp>

enum Color { RED = 2, BLUE = 4, GREEN = 8 };

Color color = Color::RED;
auto color_name = magic_enum::enum_name(color);
// color_name -> "RED"

std::string color_name{"GREEN"};
auto color = magic_enum::enum_cast<Color>(color_name)
if (color.has_value()) {
  // color.value() -> Color::GREEN
};

For more examples check home repository https://github.com/Neargye/magic_enum.

Where is the drawback?

This library uses a compiler-specific hack (based on __PRETTY_FUNCTION__ / __FUNCSIG__), which works on Clang >= 5, MSVC >= 15.3 and GCC >= 9.

Enum value must be in range [MAGIC_ENUM_RANGE_MIN, MAGIC_ENUM_RANGE_MAX].

  • By default MAGIC_ENUM_RANGE_MIN = -128, MAGIC_ENUM_RANGE_MAX = 128.

  • If need another range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MIN and MAGIC_ENUM_RANGE_MAX.

  • MAGIC_ENUM_RANGE_MIN must be less or equals than 0 and must be greater than INT16_MIN.

  • MAGIC_ENUM_RANGE_MAX must be greater than 0 and must be less than INT16_MAX.

  • If need another range for specific enum type, add specialization enum_range for necessary enum type.

    #include <magic_enum.hpp>
    
    enum number { one = 100, two = 200, three = 300 };
    
    namespace magic_enum {
    template <>
      struct enum_range<number> {
        static constexpr int min = 100;
        static constexpr int max = 300;
    };
    }
    

這篇關于在現(xiàn)代 C++11/C++14/C++17 和未來的 C++20 中枚舉到字符串的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

boost_1_60_0 .zip installation in windows(Windows 中的 boost_1_60_0 .zip 安裝)
How do I get console output in C++ with a Windows program?(如何使用 Windows 程序在 C++ 中獲得控制臺輸出?)
How do I calculate the week number given a date?(如何計算給定日期的周數(shù)?)
OpenCV with Network Cameras(帶有網(wǎng)絡攝像機的 OpenCV)
Export all symbols when creating a DLL(創(chuàng)建 DLL 時導出所有符號)
Getting started with OpenCV 2.4 and MinGW on Windows 7(Windows 7 上的 OpenCV 2.4 和 MinGW 入門)
主站蜘蛛池模板: 久久精品观看 | 欧美日韩一区二区在线 | 69堂视频 | 国产成人午夜高潮毛片 | 91成人在线视频 | 国产黄色精品视频 | 亚洲天堂网址 | 天天插天天透 | 亚洲色欲色欲www在线观看 | 欧美视频免费看 | 久久久久一区二区三区 | 91久久国产综合久久91精品网站 | 中文字幕伊人 | 国产福利小视频 | 在线看91 | 中文字幕亚洲欧美 | 亚洲欧美乱综合图片区小说区 | 精品在线免费观看 | 日韩精品视频免费在线观看 | 成人在线网址 | 亚洲国产成人在线 | 亚洲www啪成人一区二区麻豆 | 日韩a视频 | 欧美精品99 | 成人免费在线观看网站 | 一区二区三区高清 | 午夜快播| 97超碰在线免费观看 | 日韩a在线观看 | 日韩在线视频播放 | 欧美做受 | 日韩三级一区 | 亚洲一区成人 | 国产在线不卡视频 | 久久精品视频网站 | 亚洲精品美女 | 亚洲三级网站 | av噜噜| 精品一二三 | 黄色午夜| 91最新视频|