Enum in C,C++
C: C11 以上
第一种方式:言语成了抢功尽责的功臣,还要行为来值班吗
/** * @file duLangMap.h * @author geovindu,Geovin Du(geovindu@163.com) * @brief vscode c11 * @version 0.1 * @date 2023-12-18 * * @copyright Copyright (c) 站在巨人的肩膀上 Standing on the Shoulders of Giants 2023 * */ #ifndef DULANGMAP_H #define DULANGMAP_H #define ChinseLangsList \ DUX(afdu) \ DUX(aldu) \ DUX(ardu) \ DUX(azdu) \ DUX(bgdu) \ DUX(cadu) \ DUX(czdu) \ DUX(dadu) \ DUX(dedu) \ DUX(eldu) \ DUX(endu) \ DUX(eudu) \ DUX(fadu) \ DUX(fidu) \ DUX(frdu) \ DUX(gldu) \ DUX(hedu) \ DUX(hidu) \ DUX(hrdu) \ DUX(hudu) \ DUX(iddu) \ DUX(itdu) \ DUX(jadu) \ DUX(krdu) \ DUX(ladu) \ DUX(ltdu) \ DUX(mkdu) \ DUX(nodu) \ DUX(nldu) \ DUX(pldu) \ DUX(ptdu) \ DUX(pt_brdu) \ DUX(rodu) \ DUX(rudu) \ DUX(svdu) \ DUX(sedu) \ DUX(skdu) \ DUX(sldu) \ DUX(spdu) \ DUX(esdu) \ DUX(srdu) \ DUX(thdu) \ DUX(trdu) \ DUX(uadu) \ DUX(ukdu) \ DUX(vidu) \ DUX(zh_cndu) \ DUX(zh_twdu) \ DUX(zudu) typedef enum { #define DUX(name) name, ChinseLangsList #undef DUX Chinse_Count } numbers_t; /** * @brief * */ static const char* LangDuToString[Chinse_Count] = { #define DUX(name) [name] = #name, ChinseLangsList #undef DUX }; #endif
第二种方式:
/** * @file LanguageList.h * @author geovindu,Geovin Du,涂聚文 (geovindu@163.com) * @brief * IDE: vscode c11 * @version 0.1 * @date 2023-12-18 * * @copyright Copyright (c) 站在巨人的肩膀上 Standing on the Shoulders of Giants 2023 * */ #ifndef LANGUAGELIST_H #define LANGUAGELIST_H #define EnumLangTypeCase(x) case x: return(#x); /** * @brief 语种类型 * */ enum DuLang { af= 0,// Afrikaans al=1 ,// Albanian ar=2 ,// Arabic az=3 ,// Azerbaijani bg=4 ,// Bulgarian ca=5 ,// Catalan cz=6 ,// Czech da=7 ,// Danish de=8 ,// German el=9 ,// Greek en=10 ,// English eu=11 ,// Basque fa=12 ,// Persian (Farsi) fi=13,// Finnish fr=14,// French gl=15 ,// Galician he=16 ,// Hebrew hi=17,// Hindi hr=18 ,// Croatian hu=19,// Hungarian id=20,// Indonesian it=21,// Italian ja=22 ,// Japanese kr=23 ,// Korean la=24,// Latvian lt=25,// Lithuanian mk=26 ,// Macedonian no=27 ,// Norwegian nl=28 ,// Dutch pl=29,// Polish pt=30,// Portuguese pt_br=31,// Português Brasil ro=32,// Romanian ru=33,// Russian sv=34 ,// se Swedish se=35, sk=36 ,// Slovak sl=37 ,// Slovenian sp=38 ,// es Spanish es=39, sr=40 ,// Serbian th=41 ,// Thai tr=42,// Turkish ua=43,// uk Ukrainian uk=44, vi=45,// Vietnamese zh_cn=46 ,// Chinese Simplified zh_tw=47 ,// Chinese Traditional zu=48 // Zulu }; /** * @brief * */ static inline const char *LangtoString(enum DuLang type) { switch (type){ EnumLangTypeCase(af)// Afrikaans EnumLangTypeCase(al)// Albanian EnumLangTypeCase(ar)// Arabic EnumLangTypeCase(az)// Azerbaijani EnumLangTypeCase(bg)// Bulgarian EnumLangTypeCase(ca)// Catalan EnumLangTypeCase(cz)// Czech EnumLangTypeCase(da)// Danish EnumLangTypeCase(de)// German EnumLangTypeCase(el)// Greek EnumLangTypeCase(en)// English EnumLangTypeCase(eu)// Basque EnumLangTypeCase(fa)// Persian (Farsi) EnumLangTypeCase(fi)// Finnish EnumLangTypeCase(fr)// French EnumLangTypeCase(gl)// Galician EnumLangTypeCase(he)// Hebrew EnumLangTypeCase(hi)// Hindi EnumLangTypeCase(hr)// Croatian EnumLangTypeCase(hu)// Hungarian EnumLangTypeCase(id)// Indonesian EnumLangTypeCase(it)// Italian EnumLangTypeCase(ja)// Japanese EnumLangTypeCase(kr)// Korean EnumLangTypeCase(la)// Latvian EnumLangTypeCase(lt)// Lithuanian EnumLangTypeCase(mk)// Macedonian EnumLangTypeCase(no)// Norwegian EnumLangTypeCase(nl)// Dutch EnumLangTypeCase(pl)// Polish EnumLangTypeCase(pt)// Portuguese EnumLangTypeCase(pt_br)// Português Brasil EnumLangTypeCase(ro)// Romanian EnumLangTypeCase(ru)// Russian EnumLangTypeCase(sv)// se Swedish EnumLangTypeCase(se) EnumLangTypeCase(sk)// Slovak EnumLangTypeCase(sl)// Slovenian EnumLangTypeCase(sp)// es Spanish EnumLangTypeCase(es) EnumLangTypeCase(sr)// Serbian EnumLangTypeCase(th)// Thai EnumLangTypeCase(tr)// Turkish EnumLangTypeCase(ua)// uk Ukrainian EnumLangTypeCase(uk) // EnumLangTypeCase(vi)// Vietnamese EnumLangTypeCase(zh_cn)// Chinese Simplified EnumLangTypeCase(zh_tw)// Chinese Traditional EnumLangTypeCase(zu)// Zulu } return "nodata"; } #endif
调用:
#include "include/duLangMap.h" #include "include/LanguageList.h" int main() { printf("hello world, c \n"); printf("你好,中国\n"); //简体中文 46 printf("%s\n",LangtoString(46)); printf("%s\n",LangtoString(zh_cn)); printf("%s\n",LangDuToString[2]); printf("%s\n",LangDuToString[zh_cndu]); return 0 }
输出:
第三种方式:
/** * ***************************************************************************** * @file LanguageDu.h * @brief * @author (geovindu,Geovin Du) * @date 2023-12-19 * @copyright geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants * ***************************************************************************** */ typedef enum LangNum { afd= 0,// Afrikaans ald=1 ,// Albanian ard=2 ,// Arabic azd=3 ,// Azerbaijani bgd=4 ,// Bulgarian cad=5 ,// Catalan czd=6 ,// Czech dad=7 ,// Danish ded=8 ,// German eld=9 ,// Greek end=10 ,// English eud=11 ,// Basque fad=12 ,// Persian (Farsi) fid=13,// Finnish frd=14,// French gld=15 ,// Galician hed=16 ,// Hebrew hid=17,// Hindi hrd=18 ,// Croatian hud=19,// Hungarian idd=20,// Indonesian itd=21,// Italian jad=22 ,// Japanese krd=23 ,// Korean lad=24,// Latvian ltd=25,// Lithuanian mkd=26 ,// Macedonian nod=27 ,// Norwegian nld=28 ,// Dutch pld=29,// Polish ptd=30,// Portuguese pt_brd=31,// Português Brasil rod=32,// Romanian rud=33,// Russian svd=34 ,// se Swedish sed=35, skd=36 ,// Slovak sld=37 ,// Slovenian spd=38 ,// es Spanish esd=39, srd=40 ,// Serbian thd=41 ,// Thai trd=42,// Turkish uad=43,// uk Ukrainian ukd=44, vid=45,// Vietnamese zh_cnd=46 ,// Chinese Simplified zh_twd=47 ,// Chinese Traditional zud=48,// Zulu Max } LangNumdu; /** * @brief * */ static const char* LangNumduToString[Max] = { [afd]="afd",// Afrikaans [ald]="ald",// Albanian [ard]="ard",// Arabic [azd]="azd",// Azerbaijani [bgd]="bgd",// Bulgarian [cad]="cad",// Catalan [czd]="czd",// Czech [dad]="dad",// Danish [ded]="ded",// German [eld]="eld",// Greek [end]="end",// English [eud]="eud",// Basque [fad]="fad",// Persian (Farsi) [fid]="fid",// Finnish [frd]="frd",// French [gld]="gld",// Galician [hed]="hed",// Hebrew [hid]="hid",// Hindi [hrd]="hrd",// Croatian [hud]="hud",// Hungarian [idd]="idd",// Indonesian [itd]="itd",// Italian [jad]="jad",// Japanese [krd]="krd",// Korean [lad]="lad",// Latvian [ltd]="ltd",// Lithuanian [mkd]="mkd",// Macedonian [nod]="nod",// Norwegian [nld]="nld",// Dutch [pld]="pld",// Polish [ptd]="ptd",// Portuguese [pt_brd]="pt_brd",// Português Brasil [rod]="rod",// Romanian [rud]="rud",// Russian [svd]="svd",// se Swedish [sed]="sed", [skd]="skd",// Slovak [sld]="sld",// Slovenian [spd]="spd",// es Spanish [esd]="esd", [srd]="srd",// Serbian [thd]="thd",// Thai [trd]="trd",// Turkish [uad]="uad",// uk Ukrainian [ukd]="ukd", [vid]="vid",// Vietnamese [zh_cnd]="zh_cnd",// Chinese Simplified [zh_twd]="zh_twd",// Chinese Traditional [zud]="zud"// Zulu };
调用:
printf("%s\n",LangNumduToString[46]); printf("%s\n",LangNumduToString[zh_cnd]);
C++: C++14 以上
第一种方式:
/*****************************************************************//** * \file LanguageList.h * \brief * https://belaycpp.com/2021/08/24/best-ways-to-convert-an-enum-to-a-string/ * \ IDE vs 2022 C++ 20 * \author geovindu * \date December 18 2023 * \copyright geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants *********************************************************************/ #pragma once #ifndef LANGUAGELIST_H #define LANGUAGELIST_H #include <iostream> /** * \brief * */ enum class Lang //:string .net 8 { af,// Afrikaans al,// Albanian ar,// Arabic az,// Azerbaijani bg,// Bulgarian ca,// Catalan cz,// Czech da,// Danish de,// German el,// Greek /// <summary> /// 英文 /// </summary> en,// English eu,// Basque fa,// Persian (Farsi) fi,// Finnish fr,// French gl,// Galician he,// Hebrew hi,// Hindi hr,// Croatian hu,// Hungarian id,// Indonesian it,// Italian ja,// Japanese kr,// Korean la,// Latvian lt,// Lithuanian mk,// Macedonian no,// Norwegian nl,// Dutch pl,// Polish pt,// Portuguese pt_br,// Português Brasil ro,// Romanian ru,// Russian sv,// se Swedish se, sk,// Slovak sl,// Slovenian sp,// es Spanish es, sr,// Serbian th,// Thai tr,// Turkish ua,// uk Ukrainian uk, vi,// Vietnamese /// <summary> /// 简体中文 /// </summary> zh_cn,// Chinese Simplified /// <summary> /// 繁体中文 /// </summary> zh_tw,// Chinese Traditional zu// Zulu }; /** * \brief * * \param l * \return */ const char* LangToString(Lang l) noexcept { switch (l) { case Lang::af: return "af";// Afrikaans case Lang::al: return "al";// Albanian case Lang::ar: return "ar";// Arabic case Lang::az: return "az";// Azerbaijani case Lang::bg: return "bg";// Bulgarian case Lang::ca: return "ca";// Catalan case Lang::cz: return "cz";// Czech case Lang::da: return "da";// Danish case Lang::de: return "de";// German case Lang::el: return "el";// Greek case Lang::en: return "en";// English case Lang::eu: return "eu";// Basque case Lang::fa: return "fa";// Persian (Farsi) case Lang::fi: return "fi";// Finnish case Lang::fr: return "fr";// French case Lang::gl: return "gl";// Galician case Lang::he: return "he";// Hebrew case Lang::hi: return "hi";// Hindi case Lang::hr: return "hr";// Croatian case Lang::hu: return "hu";// Hungarian case Lang::id: return "id";// Indonesian case Lang::it: return "it";// Italian case Lang::ja: return "ja";// Japanese case Lang::kr: return "kr";// Korean case Lang::la: return "la";// Latvian case Lang::lt: return "lt";// Lithuanian case Lang::mk: return "mk";// Macedonian case Lang::no: return "no";// Norwegian case Lang::nl: return "nl";// Dutch case Lang::pl: return "pl";// Polish case Lang::pt: return "pt";// Portuguese case Lang::pt_br: return "pt_br";// Português Brasil case Lang::ro: return "ro";// Romanian case Lang::ru: return "ru";// Russian case Lang::sv: return "sv";// se Swedish case Lang::se: return "se"; case Lang::sk: return "sk";// Slovak case Lang::sl: return "sl";// Slovenian case Lang::sp: return "sp";// es Spanish case Lang::es: return "es"; case Lang::sr: return "sr";// Serbian case Lang::th: return "th";// Thai case Lang::tr: return "tr";// Turkish case Lang::ua: return "ua";// uk Ukrainian case Lang::uk: return "uk"; case Lang::vi: return "vi";// Vietnamese case Lang::zh_cn: return "zh_cn";// Chinese Simplified case Lang::zh_tw: return "zh_tw";// Chinese Traditional case Lang::zu: return "zu";// Zulu default: throw std::invalid_argument("nodata"); } } #endif
调用:
#include "LanguageList.h" using namespace std; int main() { std::cout << "Hello World!涂聚文 Geovin Du\n"; std::cout<< LangToString(Lang::zh_cn)<< std::endl; return 0; }
第二种方式:
/*****************************************************************//** * \file DuLanguage.h * \brief 语言种类 * * \ IDE vs 2022 C++ 20 * \author geovindu * \date December 18 2023 * \copyright geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants *********************************************************************/ #pragma once #ifndef DULANGUAGE_H #define DULANGUAGE_H #include <iostream> /** * @brief . */ #define DuLang(name, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25,v26,v27,v28,v29,v30,v31,v32,v33,v34,v35,v36,v37,v38,v39,v40,v41,v42,v43,v44,v45,v46,v47,v48,v49)\ enum name { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25,v26,v27,v28,v29,v30,v31,v32,v33,v34,v35,v36,v37,v38,v39,v40,v41,v42,v43,v44,v45,v46,v47,v48,v49 };\ const char *name##Strings[] = { #v1, #v2, #v3, #v4, #v5, #v6, #v7, #v8, #v9, #v10,#v11,#v12,#v13,#v14,#v15,#v16,#v17,#v18,#v19,#v20,#v21,#v22,#v23,#v24,#v25,#v26,#v27,#v28,#v29,#v30,#v31,#v32,#v33,#v34,#v35,#v36,#v37,#v38,#v39,#v40,#v41,#v42,#v43,#v44,#v45,#v46,#v47,#v48,#v49 };\ const char *name##ToString(int value) { return name##Strings[value]; } /** * @brief 语言类型. */ DuLang(dLang, af,// Afrikaans al,// Albanian ar,// Arabic az,// Azerbaijani bg,// Bulgarian ca,// Catalan cz,// Czech da,// Danish de,// German el,// Greek /** * \brief 英语. */ en,// English eu,// Basque fa,// Persian (Farsi) fi,// Finnish fr,// French gl,// Galician he,// Hebrew hi,// Hindi hr,// Croatian hu,// Hungarian id,// Indonesian it,// Italian ja,// Japanese kr,// Korean la,// Latvian lt,// Lithuanian mk,// Macedonian no,// Norwegian nl,// Dutch pl,// Polish pt,// Portuguese pt_br,// Português Brasil ro,// Romanian ru,// Russian sv,// se Swedish se, sk,// Slovak sl,// Slovenian sp,// es Spanish es, sr,// Serbian th,// Thai tr,// Turkish ua,// uk Ukrainian uk, vi,// Vietnamese /** * @brief 简体中文. */ zh_cn,// Chinese Simplified /** * @brief 繁体中文. */ zh_tw,// Chinese Traditional zu// Zulu ); #endif
调用:
#include "DuLanguage.h" using namespace std; int main() { std::cout << "Hello World!涂聚文 Geovin Du\n"; //int a = 2; //std::cout<< LangToString(Lang::zh_cn)<< std::endl; std::cout << dLangToString(dLang::zh_cn)<< std::endl; return 0; }
输出:
第三种方式:
/*****************************************************************//** * \file ChineseLang.h * \brief 语言类型枚举 * https://stackoverflow.com/questions/201593/is-there-a-simple-way-to-convert-c-enum-to-string * \ IDE vs 2022 C++ 20 * \author geovindu * \date December 18 2023 * \copyright geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants *********************************************************************/ #pragma once #ifndef CHINESELANG_H #define CHINESELANG_H /** * @brief,不可以再用.如果有类似的语言类型枚举,这个不可以用,互相冲突 * 格式关键 */ #define DEF_ChinseLang \ Du(af, "af") \ Du(al, "al") \ Du(ar, "ar") \ Du(az, "az") \ Du(bg, "bg") \ Du(ca, "ca") \ Du(cz, "cz") \ Du(da, "da") \ Du(de, "de") \ Du(el, "el") \ Du(en, "en") \ Du(eu, "eu") \ Du(fa, "fa") \ Du(fi, "fi") \ Du(fr, "fr") \ Du(gl, "gl") \ Du(he, "he") \ Du(hi, "hi") \ Du(hr, "hr") \ Du(hu, "hu") \ Du(id, "id") \ Du(it, "it") \ Du(ja, "ja") \ Du(kr, "kr") \ Du(la, "la") \ Du(lt, "lt") \ Du(mk, "mk") \ Du(no, "no") \ Du(nl, "nl") \ Du(pl, "pl") \ Du(pt, "pt") \ Du(pt_br, "pt_br") \ Du(ro, "ro") \ Du(ru, "ru") \ Du(sv, "sv") \ Du(se, "se") \ Du(sk, "sk") \ Du(sl, "sl") \ Du(sp, "sp") \ Du(es, "es") \ Du(sr, "sr") \ Du(th, "th") \ Du(tr, "tr") \ Du(ua, "ua") \ Du(uk, "uk") \ Du(vi, "vi") \ Du(zh_cn, "zh_cn") \ Du(zh_tw, "zh_tw") \ Du(zu, "zu") enum ChinseLang { #define Du(name, str) name, DEF_ChinseLang #undef Du CHINESE_count }; /** * @brief. * * \param c * \return */ const char* ChinseLangToStr(ChinseLang c) { switch (c) { #define Du(name, str) case name : return str; DEF_ChinseLang #undef Du default: return ""; } } #endif
调用:
#include "ChineseLang.h" int main() { std::cout << "Hello C++ World! 涂聚文 Geovin Du\n"; std::cout << ChinseLangToStr(ChinseLang(2)) << std::endl; //0 索引开始 std::cout << ChinseLangToStr(ChinseLang(al)) << std::endl; return 0; }
输出:
from:
https://en.cppreference.com/w/c/preprocessor/replace
https://en.cppreference.com/w/c/language/enum
https://codereview.stackexchange.com/questions/237410/macro-to-generate-an-enum-and-an-array-of-strings
https://codereview.stackexchange.com/questions/278448/discussing-approaches-for-converting-enum-to-string
https://kubyshkin.name/posts/c-language-enums-tips-and-tricks/
https://belaycpp.com/2021/08/24/best-ways-to-convert-an-enum-to-a-string/
1982 年黨章和新憲法對四項基本原則的確認,引發憲法學界開始思考什麼是憲法的基本原則以及四項基本原則是否為憲法的基本原則等問題。這一時期中國學界對憲法原則的研究出現了較大發展。一方面,各種對憲法原則進行討論的文獻數量增多,出現了專門對憲法原則進行探討的文章,多數憲法學教材、講義均設獨立的章或節探討憲法原則,改變了對憲法原則進行分散介紹和批判的局面,代之於對憲法原則進行整體性的論述;另一方面,探討的內容包括對憲法原則與相關概念的關係,對資本主義國家和社會主義國家憲法分別確定和遵循哪些基本原則進行了歸納,對各原則進行論述、社會主義憲法原則與資本主義憲法原則之間關係等內容。
--《共和國六十年法學論爭實錄--憲法卷》 江平總主編 廈門大學出版社 2009年10月
蕭公權先生曾論及帝制國家控制其臣民的方式,主要有三:
一是通過照顧臣民基本的物質需求,避免臣民因難以忍受艱困的生活“挺而走險”;二則通過向臣民反復灌輸精心篩選的道德教條(大部分是從儒家學說中篩選出來的),使臣民接受或認同現存的統治秩序;三則通過不斷監視臣民,從而查出“奸民”並及時加以處理。
張榮明先生則認為:西周時代行政組織的主體是以宗族為機制的血緣組織,而“以地緣為基礎的行政區域的劃分是西周血緣組織的崩潰的結果,是春秋戰國社會變革形成的社會制度”。
--《中國古代鄉裡制度研究》 魯西奇著,北京大學出版社 2021年4月
在其一般意義上,政府正義,即政府對社會公共事務進行管理的一種合理狀態,意味著政府不妨礙且促進權利和義務、資源與利益在社會成員之間的適當安排和合理分配。在其法哲學層面,政府正義的核心命題是“如何通過一個‘善’的政治過程及制度建設實現整個社會的正義或者一個正義的社會”在這個意義上,政府正義包含了兩層含義:一是政府本身應當是正義的一包括政府的產生和運行過程都應當合乎正義原理;二是政府是應當為實現正義而存在的--社會正義的實現,是需要政府這個存在的,而政府存在的根本價值,正在于實現社會正義。
--法治政府要論叢書 《法治政府要論--基本原理》江國華 著 武漢大學出版社 2020年12月