【问题标题】:GCC : Unscoped enumeration type give an ambiguity errorGCC:无范围的枚举类型给出了歧义错误
【发布时间】:2018-03-22 22:48:02
【问题描述】:

在下面的代码中,我定义了一个带有long long 类型的unscoped enumeration。该程序在 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);
}

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)
      ^~~

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

【问题讨论】:

    标签: c++ gcc enums c++14 language-lawyer


    【解决方案1】:

    GCC 错了。

    无范围枚举类型转换为其基础类型被限定为integral promotion

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

    Var 转换为int 时,它还需要一个integral conversion(从long longint)。 积分推广overload resolution中排名高于积分转换

    2) 提升:积分提升、浮点提升

    3) 转换:整数转换、浮点转换、 浮点整数转换、指针转换、指向成员的指针 转换、布尔转换、派生的用户定义转换 类到它的基础

    那么fun(long long ll)应该更好匹配。


    Here 的 gcc 错误报告;它已在 2017-10-24 修复。 LIVE

    【讨论】:

      猜你喜欢
      • 2019-06-15
      • 2019-08-29
      • 1970-01-01
      • 2017-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多