为什么这个整数隐式转换不明确?

Why is this integer implicit conversion ambiguous?

我有以下简单的示例代码,class 两个整数类型的构造函数:

struct Y
{
  Y(int) {}
  Y(long long) {}
};

调用自:

void foo()
{
  char          _char;
  short         _short;
  int           _int;
  long          _long;
  long long     _longlong;

  Y y0 = _char;
  Y y1 = _short;
  Y y2 = _int;
  Y y3 = _long;
  Y y4 = _longlong;
}

当我在 64 位模式下用 gcc 4.8.4 编译它时,我只得到一个错误:

error: conversion from ‘long int’ to ‘Y’ is ambiguous
   Y y3 = _long;
          ^

我不明白的是为什么允许 _char_short 转换,但 _long 不明确。

在确定最佳可行候选时,转化类型分为三个等级:Exact Match、Promotion(包括积分推广)和Conversion(包括积分转化)。

任何小于int的整数类型都可以提升int,其他都是转换。所以对于 charshort,晋升是最可行的候选人。对于 intlong long,我们只需选择完全匹配。

然而,对于long,我们有两个选项都涉及积分转换。没有规则可以区分这两个转换中的哪个是"better",所以我们最终会产生歧义。您必须自己将 long 转换为所需的类型。