C++ 非作用域枚举的显式算子及其';s底层类型

C++ 非作用域枚举的显式算子及其';s底层类型,c++,enums,C++,Enums,将同时接受非作用域枚举及其底层类型的类声明为显式构造函数是否安全 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 我知道使用作用域

将同时接受非作用域枚举及其底层类型的类声明为显式构造函数是否安全

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

我知道使用作用域枚举是首选,但我只是想知道,标准是否能保证上述内容重载良好?

是的,根据[over.ics.scs],精确匹配优于枚举类型的整数转换。没有对枚举类型的隐式转换,任何其他整数(或非范围枚举)类型将选择
int
重载(与
short
long
使用
int
参数不明确的情况不同).

没有时间找到标准参考,所以我不会作为答案发布,但是的,这应该是定义良好的。你说的“安全”是什么意思?@Jarod42我的意思是,枚举类型总是调用
\1
,int-type总是调用
\2
。更新了代码中的注释。有点奇怪,但是给定了
struct S{operator short()const;operator category()const;};foo f4(S{})
、clang++和g++表示不明确的重载,而带有
/permissive-
的MSVC使用
S::operator category()const
foo::foo(category)
进行编译。还不确定哪个是正确的。等等,我现在不确定MSVC。它现在在戈德博尔特上表现得很怪异。