是参考转换是标准转换



c 标准表示标准转换包括

 A standard conversion sequence is a sequence of standard conversions in the following order:
(1.1) — Zero or one conversion from the following set: lvalue-to-rvalue conversion, array-to-pointer conversion,
and function-to-pointer conversion.
(1.2) — Zero or one conversion from the following set: integral promotions, floating point promotion, integral
conversions, floating point conversions, floating-integral conversions, pointer conversions, pointer to
member conversions, and boolean conversions.
(1.3) — Zero or one qualification conversion.

,但它没有计时参考转换。在C 中广泛使用;喜欢:

auto ex = std::runtime_exception();
std::exception& ref = ex; //reference conversion

//reference to Derived converion to reference to Base in
// operator = call in object slicing
class B{};
class D : public B
{};
B b = d;

我不知道为什么?

参见C 11中的§8.5.3¶4标准:

给定类型的" CV1 T1"one_answers" CV2 T2"," CV1 T1"与" CV2 T2"相关,如果T1与T2相同,或T1是T2的基类。" CV1 T1"与" CV2 T2"具有参考兼容,如果T1与T2相关,并且CV1与CV-CV2相同或更大的CV-CV2。

。 。

在您的情况下,std::exceptionstd::runtime_exception的基类,使ref引用与ex相关。由于exref都不是constvolatile,因此它们具有相同的CV-qualifience,并且refex

§8.5.3¶5:

对" CV1 T1"类型的引用是通过类型" CV2 T2"的表达来初始化的,如下所示:

- 如果参考为LVALUE参考,并且初始化器表达式

  - 是一个LVALUE(但不是位场),并且" CV1 T1"与" CV2 T2"(...)(...)

引用兼容

ref是一个lvalue参考,初始化器表达式 ex是一个lvalue,我们已经确定 refex

也是相关的是第8.5.3¶5

中的最后一句话。

在[上面引用的情况I]中,该引用被认为直接与初始化器表达式结合。

最新更新