代码如下
#include<iostream>
enum ExprValueType : uint8_t {
MinInf = 1
};
int main(int argc, char* argv[]) {
ExprValueType type = MinInf;
std::cout <<"Hello world" << std::endl;
std::cout << type << std::endl;
return 0;
}
使用 g++ abc.cc -o a.out -std=c++20
报错,报错信息如下所示
abc.cc:3:20: error: found ‘:’ in nested-name-specifier, expected ‘::’
3 | enum ExprValueType : uint8_t {
| ^
| ::
abc.cc:3:6: error: ‘ExprValueType’ has not been declared
3 | enum ExprValueType : uint8_t {
| ^~~~~~~~~~~~~
abc.cc:3:30: error: expected unqualified-id before ‘{’ token
3 | enum ExprValueType : uint8_t {
| ^
abc.cc: In function ‘int main(int, char**)’:
abc.cc:8:3: error: ‘ExprValueType’ was not declared in this scope
8 | ExprValueType type = MinInf;
| ^~~~~~~~~~~~~
abc.cc:10:16: error: ‘type’ was not declared in this scope; did you mean ‘std::__cmp_cat::type’?
10 | std::cout << type << std::endl;
| ^~~~
| std::__cmp_cat::type
In file included from /usr/include/c++/13/bits/char_traits.h:56,
from /usr/include/c++/13/ios:42,
from /usr/include/c++/13/ostream:40,
from /usr/include/c++/13/iostream:41,
from abc.cc:1:
/usr/include/c++/13/compare:49:11: note: ‘std::__cmp_cat::type’ declared here
49 | using type = signed char;
| ^~~~
个人猜测是 gcc 版本的问题,于是在 https://gcc.godbolt.org/ 切换不同的编译器查看。
于是产生了两个困惑
1
xipuxiaoyehua 2023-11-07 23:52:52 +08:00 via iPhone
enum class ExprValueType : uint8_t {
MinInf = 1 }; |
2
Betsy OP @xipuxiaoyehua 不可以的,从 Unscoped enumerations 换成 Scoped enumerations 依然存在这个问题。
|
3
geelaw 2023-11-08 01:07:06 +08:00
因为标准规定 std::uint8_t 在 cstdint 中定义。请注意直接用 uint8_t 也是错误的,能编译通过只是因为运气好。
之前在 #include <iostream> 之后看起来可以用,大概是因为: 1. 具体实现里的 iostream 间接包含了 cstdint ;且 2. 具体实现里的 cstdint 也在全局命名空间里定义了 uint8_t 。 这两件事不是必然成立,所以编译失败不稀奇。 |
4
marat1ren 2023-11-08 01:08:16 +08:00 via iPhone
感觉是需要 cstdint 里面定义的 uint8_t 。可以试试 std::unit8_t
|
6
dangyuluo 2023-11-08 02:39:47 +08:00
|
7
dangyuluo 2023-11-08 02:39:53 +08:00
缺 uint8_t
|
8
Betsy OP |