Skip to content
Snippets Groups Projects
Unverified Commit 2ad7b933 authored by Houjiang Chen's avatar Houjiang Chen Committed by GitHub
Browse files

Add tag for Optional inplace constructor (#5619)


* Fix optional

* update

Co-authored-by: default avataroneflow-ci-bot <69100618+oneflow-ci-bot@users.noreply.github.com>
parent e8808b18
No related branches found
No related tags found
No related merge requests found
......@@ -21,7 +21,6 @@ limitations under the License.
#include "oneflow/core/common/maybe.h"
namespace oneflow {
namespace internal {
template<typename T, typename U = void>
......@@ -108,14 +107,19 @@ class Storage<T, typename std::enable_if<!IsScalarType<T>::value>::type> {
} // namespace internal
template<typename T>
class Optional {
class Optional final {
private:
template<typename U>
using is_self = std::is_same<Optional, typename std::decay<U>::type>;
public:
Optional() : init_(false) {}
template<typename... Args,
typename std::enable_if<std::is_constructible<internal::Storage<T>, Args...>::value,
template<typename U,
typename std::enable_if<!is_self<U>::value
&& std::is_constructible<internal::Storage<T>, U>::value,
int>::type = 0>
Optional(Args&&... args) : init_(true), storage_(std::forward<Args>(args)...) {}
Optional(U&& val) : init_(true), storage_(std::forward<U>(val)) {}
~Optional() = default;
......@@ -165,7 +169,7 @@ class Optional {
};
template<typename T>
class Optional<T&> {
class Optional<T&> final {
public:
Optional() : value_ptr_(nullptr) {}
......@@ -173,11 +177,6 @@ class Optional<T&> {
~Optional() = default;
Optional& operator=(T& val) {
value_ptr_ = &val;
return *this;
}
Optional& operator=(const Optional<T&>& rhs) {
value_ptr_ = rhs.value_ptr_;
return *this;
......@@ -188,8 +187,6 @@ class Optional<T&> {
return *value_ptr_;
}
void Clear() { value_ptr_ = nullptr; }
bool has_value() const { return value_ptr_ != nullptr; }
operator bool() const { return has_value(); }
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment