这是 ConvertsWithoutNarrowing 的正确实现吗
Is this a correct implementation of ConvertsWithoutNarrowing
我目前正在学习 C++20 中的概念,遇到了这个例子:
template <typename From, typename To>
concept is_convertible_without_narrowing = requires (From&& from) {
{ std::type_identity_t<To[]>{std::forward<From>(from)}} -> std::same_as<To[1]>;
};
我很好奇是否可以将以下内容视为上述内容的正确替代实现:
template <typename From, typename To>
concept is_convertible_without_narrowing = requires (From&& from) {
{ To{std::forward<From>(from)} } -> std::same_as<To>;
}
或者,更简单:
template <typename From, typename To>
concept is_convertible_without_narrowing = requires (From&& from, To&& to) {
to = {from};
}
I am curious if the following can be considered a correct alternative
implementation of the above:
简单的答案是否定的。
在第二个版本中,To{std::forward<From>(from)}
可以看作是通过initializer_list
构造To
,所以is_convertible_without_narrowing<int, std::vector<int>>
就是true
。
同理,第三版可以看成是To
赋值给initializer_list
,所以is_convertible_without_narrowing<int, std::vector<int>&>
也会是true
.
我目前正在学习 C++20 中的概念,遇到了这个例子:
template <typename From, typename To>
concept is_convertible_without_narrowing = requires (From&& from) {
{ std::type_identity_t<To[]>{std::forward<From>(from)}} -> std::same_as<To[1]>;
};
我很好奇是否可以将以下内容视为上述内容的正确替代实现:
template <typename From, typename To>
concept is_convertible_without_narrowing = requires (From&& from) {
{ To{std::forward<From>(from)} } -> std::same_as<To>;
}
或者,更简单:
template <typename From, typename To>
concept is_convertible_without_narrowing = requires (From&& from, To&& to) {
to = {from};
}
I am curious if the following can be considered a correct alternative implementation of the above:
简单的答案是否定的。
在第二个版本中,To{std::forward<From>(from)}
可以看作是通过initializer_list
构造To
,所以is_convertible_without_narrowing<int, std::vector<int>>
就是true
。
同理,第三版可以看成是To
赋值给initializer_list
,所以is_convertible_without_narrowing<int, std::vector<int>&>
也会是true
.