mercredi 19 octobre 2016

printf of enum classes with inttypes does not work with clang

Please consider following minimum example:

#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;
}

see also Live Example

This example compiles well with gcc, e.g. gcc 4.9.4. This example does not compile with clang, e.g. clang 3.9.0

Why is it not possible to printf enum classes that are derived from std-inttypes by just using the corresponding printf specifier, in that case PRIu8 in clang? Is that a clang compiler bug? Or do I miss a detail in the C++ standard?

The compile error is

warning: format specifies type 'unsigned int' but the argument has
underlying type 'uint8_t' (aka 'unsigned char') [-Wformat]

Aucun commentaire:

Enregistrer un commentaire