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

如何在 C++11 中輸出枚舉類的值

How can I output the value of an enum class in C++11(如何在 C++11 中輸出枚舉類的值)
本文介紹了如何在 C++11 中輸出枚舉類的值的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

限時(shí)送ChatGPT賬號(hào)..

如何在 C++11 中輸出 enum class 的值?在 C++03 中是這樣的:

How can I output the value of an enum class in C++11? In C++03 it's like this:

#include <iostream>

using namespace std;

enum A {
  a = 1,
  b = 69,
  c= 666
};

int main () {
  A a = A::c;
  cout << a << endl;
}

在 c++0x 中,此代碼無(wú)法編譯

in c++0x this code doesn't compile

#include <iostream>

using namespace std;

enum class A {
  a = 1,
  b = 69,
  c= 666
};

int main () {
  A a = A::c;
  cout << a << endl;
}


prog.cpp:13:11: error: cannot bind 'std::ostream' lvalue to 'std::basic_ostream<char>&&'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/ostream:579:5: error:   initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char, _Traits = std::char_traits<char>, _Tp = A]'

編譯于 Ideone.com

推薦答案

與無(wú)作用域枚舉不同,有作用域枚舉不能隱式轉(zhuǎn)換為其整數(shù)值.您需要顯式使用強(qiáng)制轉(zhuǎn)換將其轉(zhuǎn)換為整數(shù):

Unlike an unscoped enumeration, a scoped enumeration is not implicitly convertible to its integer value. You need to explicitly convert it to an integer using a cast:

std::cout << static_cast<std::underlying_type<A>::type>(a) << std::endl;

您可能希望將邏輯封裝到函數(shù)模板中:

You may want to encapsulate the logic into a function template:

template <typename Enumeration>
auto as_integer(Enumeration const value)
    -> typename std::underlying_type<Enumeration>::type
{
    return static_cast<typename std::underlying_type<Enumeration>::type>(value);
}

用作:

std::cout << as_integer(a) << std::endl;

這篇關(guān)于如何在 C++11 中輸出枚舉類的值的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡(jiǎn)單指針的區(qū)別?)
Difference between const. pointer and reference?(常量之間的區(qū)別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問(wèn)向量的內(nèi)容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對(duì)普通變量進(jìn)行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會(huì)導(dǎo)致訪問(wèn)沖突?)
主站蜘蛛池模板: 欧美一区视频在线 | 日日日操 | 国产羞羞视频在线观看 | 在线视频中文字幕 | 久久人人爽人人爽人人片av免费 | 欧美一a | 一区二区三区在线看 | 国产三级精品三级在线观看四季网 | 成人性视频在线播放 | 久久久精品国产 | 在线国产精品一区 | 丝袜久久 | 91p在线观看 | 久久久久国 | 四虎海外| 中文字幕在线一区 | 午夜久久久久久久久久一区二区 | www.色五月.com | 国内毛片毛片毛片毛片 | 欧美另类日韩 | 美女视频黄色片 | 亚洲成人免费 | 天堂男人av | 欧美最猛黑人 | 国产精品久久国产精品 | 精品视频久久久久久 | 综合五月婷| 免费视频一区二区 | 91一区二区三区在线观看 | 亚洲一区二区视频 | 国产精品一区一区 | 天天干天天玩天天操 | 久久草在线视频 | 久久91| 亚洲国产视频一区二区 | 欧美性猛交一区二区三区精品 | 亚洲高清免费观看 | 国产农村一级片 | 日日干夜夜操 | 国产一级片 | 日韩成人免费av |