有没有办法使“enum”类型变为无符号?

发布于 2024-08-31 00:38:27 字数 530 浏览 3 评论 0原文

有没有办法使 enum 类型变为无符号?以下代码向我发出有关有符号/无符号比较的警告。

enum EEE {
    X1 = 1
};

int main()
{
    size_t x = 2;
    EEE t = X1;
    if ( t < x ) std::cout << "ok" << std::endl;

    return 0;
}

我试图强制编译器使用以下枚举的无符号基础类型:

enum EEE {
    X1 = 1,
    XN = 18446744073709551615LL
    // I've tried XN = UINT_MAX (in Visual Studio). Same warning.
};

但这仍然给出警告。


将常量更改为 UINT_MAX 使其可以按照标准在 GNU C++ 中工作。看来是VS的一个bug。感谢詹姆斯的提示。

Is there a way to make enum type to be unsigned? The following code gives me a warning about signed/unsigned comparison.

enum EEE {
    X1 = 1
};

int main()
{
    size_t x = 2;
    EEE t = X1;
    if ( t < x ) std::cout << "ok" << std::endl;

    return 0;
}

I've tried to force compiler to use unsigned underlying type for enum with the following:

enum EEE {
    X1 = 1,
    XN = 18446744073709551615LL
    // I've tried XN = UINT_MAX (in Visual Studio). Same warning.
};

But that still gives the warning.


Changing constant to UINT_MAX makes it working in GNU C++ as should be according to the standard. Seems to be a bug in VS. Thanks to James for hint.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

握住我的手 2024-09-07 00:38:27

您可以尝试:

enum EEE {
    X1 = 1,
    XN = -1ULL
};

如果没有 U,则整数文字是有符号的。

(当然,假设您的实现支持 long long;我认为它是这样的,因为原始问题使用 LL;否则,您可以使用 UL一个)。

You might try:

enum EEE {
    X1 = 1,
    XN = -1ULL
};

Without the U, the integer literal is signed.

(This of course assumes your implementation supports long long; I assume it does since the original question uses LL; otherwise, you can use UL for a long).

清旖 2024-09-07 00:38:27

当前版本的 C++ 中没有。 C++0x 将提供强类型枚举。

目前,您可以使用 if ( static_cast(t) < x ) 来删除警告。

Not in the current version of C++. C++0x will provide strongly typed enums.

For the time being, you can use if ( static_cast<size_t>(t) < x ) to remove the warning.

甜味超标? 2024-09-07 00:38:27

如果您想比较它,您也可以重载运算符,

enum EEE {
    X1 = 1
};

bool operator<(EEE e, std::size_t u) {
  return (int)e < (int)u;
}

但是您必须对右侧的任何整数类型执行此操作。否则,如果你这样做 e < 2 这将是不明确的:编译器可以使用与左侧完全匹配但需要在右侧进行转换的operator<,或其内置运算符,需要对左侧与右侧完全匹配。

所以最终,我会放以下版本:

/* everything "shorter" than "int" uses either int or unsigned */
bool operator<(EEE e, int u) {
  return (int)e < (int)u;
}

bool operator<(EEE e, unsigned u) {
  return (unsigned int)e < (unsigned int)u;
}


bool operator<(EEE e, long u) {
  return (long)e < (long)u;
}

bool operator<(EEE e, unsigned long u) {
  return (unsigned long)e < (unsigned long)u;
}

/* long long if your compiler has it, too */

不是很好:)但至少你的枚举的用户很随和。但是,如果您最终不想与普通的 int 进行比较,而是与一些有意义的值进行比较,我会按照其他人的建议进行操作,并添加另一个具有值 2,并为其命名。这样,警告也会消失。

You could also overload the operators if you want to compare it

enum EEE {
    X1 = 1
};

bool operator<(EEE e, std::size_t u) {
  return (int)e < (int)u;
}

However you have to do that dance for any integer type on the right side. Otherwise if you do e < 2 it would be ambiguous: The compiler could use your operator< matching the left side exactly but needing a conversion on the right side, or its built-in operator, needing a promotion for the left side and matching the rigth side exactly.

So ultimately, i would put the following versions:

/* everything "shorter" than "int" uses either int or unsigned */
bool operator<(EEE e, int u) {
  return (int)e < (int)u;
}

bool operator<(EEE e, unsigned u) {
  return (unsigned int)e < (unsigned int)u;
}


bool operator<(EEE e, long u) {
  return (long)e < (long)u;
}

bool operator<(EEE e, unsigned long u) {
  return (unsigned long)e < (unsigned long)u;
}

/* long long if your compiler has it, too */

Not very nice :) But at least the user of your enumeration has easy going. However if you ultimately don't want to compare against ordinary int but against some meaningful value, i would do what some other guy proposed, and add another enumerator that has as value 2, and name it. That way, warnings will go away too.

吐个泡泡 2024-09-07 00:38:27

根据 C++ 枚举是有符号的还是无符号的?
你的编译器可以选择 enum 是否有符号,尽管有一些评论说在 C++0x 中你将能够指定它是无符号的。

According to Are C++ enums signed or unsigned?
your compiler gets to choose whether enum is signed or not, though there are some comments saying that in C++0x you will be able to specify that it is unsigned.

回心转意 2024-09-07 00:38:27

根据 MSDN 上的 C++ 枚举声明

enum EEE : unsigned {
    X1 = 1 
}; 

Per C++ Enumeration Declarations on MSDN:

enum EEE : unsigned {
    X1 = 1 
}; 
〆一缕阳光ご 2024-09-07 00:38:27

为什么不

enum EEE {
    X1 = 1,
    x = 2 // pick more descriptive name, a'course
};

或者

if ( size_t( t ) < x )

Why not

enum EEE {
    X1 = 1,
    x = 2 // pick more descriptive name, a'course
};

or

if ( size_t( t ) < x )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文