std::is_convertible_v<const type &, type> 什么时候会是假的?
When would std::is_convertible_v<const type &, type> be false?
当我看到这条规则时,我正在阅读 std::pair
的构造函数规则(如 cppreference 中所述):
This constructor is explicit if and only if std::is_convertible_v<const first_type&, first_type>
is false
or std::is_convertible_v<const second_type&, second_type>
is false
.
如果 From
可以隐式转换为 To
,则 std::is_convertible_v<From, To>
为 true
,否则为 false
。
但是在什么情况下std::is_convertible_v<const T &, T>
会变成false
呢?
我已经考虑了一段时间,但实际上我想不出任何随手可得的东西。
在我看来,对 T
类型的 const 值的引用总是可以转换为 T
.
类型的值
std::is_convertible_v
检查 隐式 转换。 std::is_convertible_v<const T &, T>
returns 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
.
当我看到这条规则时,我正在阅读 std::pair
的构造函数规则(如 cppreference 中所述):
如果This constructor is explicit if and only if
std::is_convertible_v<const first_type&, first_type>
isfalse
orstd::is_convertible_v<const second_type&, second_type>
isfalse
.
From
可以隐式转换为 To
,则 std::is_convertible_v<From, To>
为 true
,否则为 false
。
但是在什么情况下std::is_convertible_v<const T &, T>
会变成false
呢?
我已经考虑了一段时间,但实际上我想不出任何随手可得的东西。
在我看来,对 T
类型的 const 值的引用总是可以转换为 T
.
std::is_convertible_v
检查 隐式 转换。 std::is_convertible_v<const T &, T>
returns 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
.