Expected<T>
实现在llvm/Support/Error.h中。它是一个标记联合,可以保持T
或Error
。
Expected<T>
是一个具有类型T
的模板类:
template <class T> class LLVM_NODISCARD Expected
但这两个构造函数真的很让我困惑:
/// 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));
}
为什么
Expected<T>
在同一实现中重复两个结构体?为什么它不像下面这样实现?template <class OtherT>
Expected(Expected<OtherT>&& Other) { moveConstruct(std::move(Other));}
explicit
关键字。 - Matexplicit
关键字?谁能给一个例子吗? - yodahaji