C++ Logo


Advanced search

Re: [std-proposals] scoped enums and varargs

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Fri, 23 Feb 2024 21:44:07 +0000
On Fri, 23 Feb 2024 at 21:32, enh via Std-Proposals
<std-proposals_at_[hidden]> wrote:
> (https://isocpp.org/std/submit-issue led me to believe there isn't a
> public bug database, and i should send a mail here.)
> clang recently started warning about using scoped enums as arguments
> to varargs functions without a cast. this caused a lot of warning spam
> in Android, and a lot of confusion as to why there's even a warning
> here.

Is this about varargs functions in general, or printf?

I think it's intentional they don't promote when passed via varargs.
Why should you get implicit conversions from scoped enumeration types
when they don't implicitly convert under any other circumstances?

So they get passed as their original type. For a general varargs
function you write yourself, there's no problem passing a scoped enum
as long as you extract it with that type:

#include <stdarg.h>

enum class E { E1 };
void f(int i ...) {
  va_list v;
  va_start(v, i);
  E e = va_arg(v, E);

int main()
  f(0, E::E1);

But for printf, you get undefined behaviour, because printf doesn't
know how to extract that type and it expects an int, a double, a
pointer, or whatever.

Received on 2024-02-23 21:45:23