Expected<T>được triển khai trong llvm / Support / Error.h. Đó là một liên minh được gắn thẻ giữ một Thoặc một Error.
Expected<T>là một lớp mẫu với loại T:
template <class T> class LLVM_NODISCARD Expected
Nhưng hai nhà xây dựng này thực sự làm tôi bối rối:
/// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT
/// must be convertible to T.
template <class OtherT>
Expected(Expected<OtherT> &&Other,
typename std::enable_if<std::is_convertible<OtherT, T>::value>::type
* = nullptr) {
moveConstruct(std::move(Other));
}
/// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT
/// isn't convertible to T.
template <class OtherT>
explicit Expected(
Expected<OtherT> &&Other,
typename std::enable_if<!std::is_convertible<OtherT, T>::value>::type * =
nullptr) {
moveConstruct(std::move(Other));
}
Tại sao Expected<T>lặp lại hai cấu trúc cho cùng thực hiện? Tại sao nó không làm như thế này?:
template <class OtherT>
Expected(Expected<OtherT>&& Other) { moveConstruct(std::move(Other));}
explicittừ khóa quan trọng ở đây? Bất cứ ai có thể đưa ra một ví dụ?
explicittừ khóa