相关疑难解决方法(0)

将enum类变量重新解释为基础类型的引用是否安全?

我已经看到过reinterpret_cast常用于对枚举类应用增量,我想知道这种用法在标准C++中是否可以接受.

enum class Foo : int8_t
{
    Bar1,
    Bar2,
    Bar3,
    Bar4,

    First = Bar1,
    Last = Bar4
};

for (Foo foo = Foo::First; foo <= Foo::Last; ++reinterpret_cast<int8_t &>(foo))
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

我知道,如果是普通的类,那么转换为基类的引用是安全的.但由于枚举类不是隐式转换为其底层类型的事件,因此我不确定上述代码是否以及如何保证在所有编译器中都能正常工作.有线索吗?

c++ reinterpret-cast c++11 enum-class

13
推荐指数
1
解决办法
2047
查看次数

如何使用枚举类值作为for循环的一部分?

我试图通过遍历枚举创建一副扑克牌SuitRank(我知道有没有遍历枚举好办法,但我没有看到另一种).我通过enum_count在每个枚举的末尾添加一个枚举器来做到这一点,其值用于表示枚举的长度和结尾.

#include <vector>

using namespace std;

enum class Suit: int {clubs, diamonds, hearts, spades, enum_count};
enum class Rank: int {one, two, three, four, five, six, seven, eight,
                nine, ten, jack, queen, king, ace, enum_count};

struct Card {
    Suit suit;
    Rank rank;
};

class Deck{
    vector<Card> cards{};
    public:
        Deck();
};

Deck::Deck() {
    // ERROR ON THE BELOW LINE
    for (Suit suit = Suit::clubs; suit < Suit::enum_count; suit++) {
        for (Rank rank = Rank::one; rank < …
Run Code Online (Sandbox Code Playgroud)

c++ enums loops c++11

7
推荐指数
1
解决办法
6033
查看次数

安全地将int转换为枚举

我想知道是否有任何聪明的技巧如何安全地将整数转换为枚举.在您投票之前,这是重复的,我不是在询问如何转换(int i; Enum e = static_cast<Enum>(i)很简单).我正在问如何安全地进行,验证结果值是否真的在枚举中.

以下代码

enum class E {
    A = 1,
    B = 2
};

int main(int, char **) {
    int i = 3;
    E e = static_cast<E>(i);
}
Run Code Online (Sandbox Code Playgroud)

将编译(AFAIK)但e不包含枚举中的有效值.我想出的最好的方式是

switch (i) {
    case 1:
        return E::A;
    case 2:
        return E::B;
    default:
        throw invalid_argument("");
}
Run Code Online (Sandbox Code Playgroud)

1)看起来不太聪明2)不能很好地扩展.我可能会把一些宏放在一起,以使这更容易,但它仍然看起来很愚蠢.

那么有没有"标准"的方法呢?

谢谢

c++ enums c++11

5
推荐指数
1
解决办法
2089
查看次数

c ++ 11 vs c ++ - 枚举差异

//c++03
enum Something
{
    S1 = 0,
    S2,
    SCOUNT
};
int arr[SCOUNT];

//c++11
enum class Something
{
    S1 = 0,
    S2,
    SCOUNT
};
int arr[(int) Something::SCOUNT];
Run Code Online (Sandbox Code Playgroud)

在这种情况下如何在不将枚举计数转换为int的情况下使用枚举?

c++ enums c++11 strongly-typed-enum

3
推荐指数
2
解决办法
462
查看次数

如何静态断言枚举元素的值?

例如:

enum class MyEnum { A, B };
static_assert(A == 0 && B == 1); // error: expected constructor, destructor, or type conversion before '(' token
Run Code Online (Sandbox Code Playgroud)

我该如何实现这一目标?

c++

2
推荐指数
1
解决办法
3193
查看次数

如何将Enum类型的元素插入到uint8_t类型的向量中?

我对 C++ 编程非常陌生。我想将枚举类型的元素插入到vector<uint8_t>? std::vector <ValType> call即追加到 的所有元素std::vector<uint8_t> bravo 。有什么办法可以做到这一点吗?

#include <stdio.h>
#include <vector>
#include <cstdint>

enum class ValType : uint8_t
{

    Working = 1,
    Failed = 0,
    Freezed = 0

};

int main()
{
    std::vector<uint8_t> bravo = {23, 23, 23, 22, 5};
    std::vector<ValType> call;
    bravo.insert(bravo.end(), call.begin(), call.end());

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在这里

我在编译时遇到错误:

   In file included from c:\program files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\include\c++\11.2.0\vector:66,
                 from custom.cpp:2:
c:\program files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\include\c++\11.2.0\bits\stl_uninitialized.h: In instantiation of '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<ValType*, …
Run Code Online (Sandbox Code Playgroud)

c++ enums vector c++11 c++17

0
推荐指数
1
解决办法
362
查看次数