GCC:Unscoped枚举类型给出了歧义错误

msc*_*msc 9 c++ enums gcc language-lawyer c++14

在下面的代码中,我定义了一个带有类型的unscoped枚举long long.这个程序在Clang上工作正常.

GCC编译器给出了模糊错误.

#include <iostream>

enum : long long { Var=5 };

void fun(long long ll)
{
    std::cout << "long long : " << ll << std::endl;
}

void fun(int i)
{
    std::cout << "int : " << i <<  std::endl;
}

int main()
{
    fun(Var);
}
Run Code Online (Sandbox Code Playgroud)

GCC生成错误:

main.cpp: In function 'int main()':
main.cpp:17:12: error: call of overloaded 'fun(<unnamed enum>)' is ambiguous
     fun(Var);
            ^
main.cpp:5:6: note: candidate: void fun(long long int)
 void fun(long long ll)
      ^~~
main.cpp:10:6: note: candidate: void fun(int)
 void fun(int i)
      ^~~
Run Code Online (Sandbox Code Playgroud)

为什么GCC编译器会出现歧义错误?

son*_*yao 13

GCC错了.

转换为其基础类型的无范围枚举类型被限定为整数提升:

其基础类型固定的未作用域枚举类型可以转换为其基础类型,...(从C++ 11开始)

虽然Var转换为int它需要一个更多的积分转换(从long longint).在重载决策中,积分促销的排名高于积分转换:

2)推广:整体推广,浮点推广

3)转换:积分转换,浮点转换,浮点积分转换,指针转换,指针到成员转换,布尔转换,派生类到其基数的用户定义转换

fun(long long ll)应该是更好的匹配.


是gcc bug报告; 它被修复在2017-10-24.生活