带有inttype的枚举类的Printf不能在clang中工作
printf of enum classes with inttypes does not work with clang
请考虑以下最小示例:
#include <cstdint>
#include <cstdio>
#include <cinttypes>
#include <type_traits>
enum class Foo : uint8_t
{
John,
Jane
};
int main()
{
// does not compile with clang but works fine with gcc
printf("here is my %" PRIu8 "n", Foo::John);
//with cast works fine also with clang
using T = typename std::underlying_type<Foo>::type;
printf("here is my %" PRIu8 "n", T(Foo::John));
//with cast works fine also with clang
printf("here is my %" PRIu8 "n", uint8_t(Foo::John));
return 0;
}
参见Live Example
这个例子可以很好地编译gcc,例如gcc 4.9.4。这个例子不能用clang编译,例如clang 3.9.0
为什么不可能通过使用相应的printf说明符来打印从std-inttypes派生的enum类,在这种情况下PRIu8
在clang?这是一个clang编译器的bug吗?还是我错过了c++标准中的某个细节?
编译错误
warning: format specifies type 'unsigned int' but the argument has
underlying type 'uint8_t' (aka 'unsigned char') [-Wformat]
enum class Foo
是不是一个uint8_t
,它是一个恰好使用uint8_t
作为其底层表示的枚举。如果您想将其转换为uint8_t
,请使用static_cast
。
类型系统实际上在这里试图帮助您-类型是不同的(即使是可转换的)。
相关文章:
- Clang-Format 不能正确分配函数参数
- RVO/NRVO 不能在 Clang++ 的 C++17 中禁用吗?
- clang-6 和 -std=c++17 -- 不能调用 bind(2)
- 不能继承 clang 和 gcc 中同一类的显式模板专用化
- clang vs gcc CRTP:constexpr 变量不能有非文字类型
- 使用 clang & libstdc++ 时不能使用 experimental::optional 的常量引用
- 为什么这个程序在Clang或GCC下不能正常运行?
- 同一段代码,可以用 xcode 或使用 g++ 编译,在终端中编译 clang++,不能使用 gcc 或 clang 编
- 为什么这段代码不能用 clang 构建,用 gcc 崩溃,但在 VC++ 上运行良好?
- 为什么这个模板 constexpr 函数不能在 gcc 上编译,但在 clang 上效果很好?
- gcc可以编译varadic模板,而clang不能
- 我们不能混合使用g++和clang++编译的对象(或库)?至少在Mac上是这样
- 代码不能在g++中编译,而在clang++中可以
- 带有inttype的枚举类的Printf不能在clang中工作
- 模板类中的模板迭代器,clang不能推断模板形参
- 为什么使用clang的两阶段命令行构建不能生成dSYM目录?
- 为什么这个简单的c++代码不能用clang++编译?
- c++程序不能在Clang和visual Studio 2010 Express中编译
- 一段代码不能被intel编译器编译,但是clang可以编译它
- 为什么我不能用 clang 编译这个程序?