std::byte 不是 'std' 的成员
std::byte is not member of 'std'
我正在尝试学习 c++17 的新 features/gimmicks,但后来我学习了 std::byte 并且由于某些未知原因,我似乎无法编译大多数基本 "hello world" 类型程序与类型。
整个程序:
#include <cstddef>
int main(int argc, char* argv[])
{
std::byte byte;
return 0;
}
编译命令:
g++ ./main.cpp
但输出总是:
./main.cpp: In function ‘int main(int, char**)’:
./main.cpp:4:10: error: ‘byte’ is not a member of ‘std’
std::byte byte;
我使用 gcc 7.4.0 开发 Ubuntu 18.04。我已经检查了“/usr/include/c++/7.4.0/”并且头文件 cstddef 在那里并且字节似乎被定义了。
我也试过用clang:
clang++ ./main.cpp
但结果是一样的。此时我只能认为cstddef是corrupted/bugged。有解决办法吗?
正如 πάντα ῥεῖ 在评论中指出的那样,我缺少 c++17 编译标志。右编译命令:
g++ -std=c++17 ./main.cpp
如果您使用 clang 5.0(即使使用 -std=c++17
标志)也会发生同样的错误。
在这种情况下,要解决此问题,您需要升级到 clang 6。
临时和快速的解决方法是可能的(但不推荐,因为它使用 std
命名空间),它可能是这样的:
#if defined(__clang__) && __cplusplus >= 201703L && __clang_major__ < 6
// This is a minimal workaround for clang 5.0 with missing std::byte type
namespace std {
enum class byte : unsigned char {};
}
#endif
我正在尝试学习 c++17 的新 features/gimmicks,但后来我学习了 std::byte 并且由于某些未知原因,我似乎无法编译大多数基本 "hello world" 类型程序与类型。
整个程序:
#include <cstddef>
int main(int argc, char* argv[])
{
std::byte byte;
return 0;
}
编译命令:
g++ ./main.cpp
但输出总是:
./main.cpp: In function ‘int main(int, char**)’:
./main.cpp:4:10: error: ‘byte’ is not a member of ‘std’
std::byte byte;
我使用 gcc 7.4.0 开发 Ubuntu 18.04。我已经检查了“/usr/include/c++/7.4.0/”并且头文件 cstddef 在那里并且字节似乎被定义了。
我也试过用clang:
clang++ ./main.cpp
但结果是一样的。此时我只能认为cstddef是corrupted/bugged。有解决办法吗?
正如 πάντα ῥεῖ 在评论中指出的那样,我缺少 c++17 编译标志。右编译命令:
g++ -std=c++17 ./main.cpp
如果您使用 clang 5.0(即使使用 -std=c++17
标志)也会发生同样的错误。
在这种情况下,要解决此问题,您需要升级到 clang 6。
临时和快速的解决方法是可能的(但不推荐,因为它使用 std
命名空间),它可能是这样的:
#if defined(__clang__) && __cplusplus >= 201703L && __clang_major__ < 6
// This is a minimal workaround for clang 5.0 with missing std::byte type
namespace std {
enum class byte : unsigned char {};
}
#endif