explicit ctor of unscoped-enum and it's underlying type

Viewed 48

Is it safe to declare a class that accepts both unscoped enum and its underlying type as an explicit constructor?

enum category : int {
  a, b
}

struct foo {
  explicit foo(category c) {} // #1
  explicit foo(int x) {}      // #2
};

foo f1(3);   // #2 is called
foo f2(a);   // #1 is called
foo f3((a)); // #1 is called

I know that using scoped enum is preferred, but I just wonder that does the standard guarantees that the above is well-overloaded?

1 Answers

Yes, an exact match is better than an integral conversion from the enumeration type, per [over.ics.scs]. There being no implicit conversion to an enumeration type, any other integer (or unscoped enumeration) type will select the int overload (unlike the case of short and long being ambiguous with an int argument).

Related