当我遇到此裁定时,我正在阅读std::pair
的构造函数规则(如cppreference所记录:
[且仅当
std::is_convertible_v<const first_type&, first_type>
为false
或std::is_convertible_v<const second_type&, second_type>
为false
时,此构造函数才是显式的。
如果std::is_convertible_v<From, To>
可隐式转换为true
,则[From
为To
,如果不是,则为false
。
但是在std::is_convertible_v<const T &, T>
这样的情况下,false
是什么情况?我已经考虑了一段时间了,实际上我没有想到任何附属的东西。在我看来,对T
类型的const值的引用将始终可转换为T
类型的值。
std::is_convertible_v
检查隐式转换。如果存在std::is_convertible_v<const T &, T>
的implicit复制构造函数,则true
返回T
。
struct S { explicit S(const S &) = default; };
S
具有显式副本构造函数,因此std::is_copy_constructible_v<S>
为true
,但std::is_convertible_v<const S &, S>
为false
。std::pair
的副本构造函数应为explicit
以匹配first_type
的副本构造函数,因此,当std::pair
为explicit
时,std::is_convertible_v<const first_type &, first_type>
的副本构造函数为false
是有意义的。