diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2018-06-28 13:28:22 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2018-06-28 13:28:22 +0200 |
commit | b204bd9321a2d2bd0d69fa3a8c99eef921ad5894 (patch) | |
tree | 3a508cf5167640564cbc27e58176a83499d35ea6 | |
parent | 653a6a01a64df2f7ad07c44e572dfaf92ee8ad4b (diff) |
Make optional copy and move constructors constexpr
-rw-r--r-- | libbutl/optional.ixx | 36 | ||||
-rw-r--r-- | libbutl/optional.mxx | 33 |
2 files changed, 21 insertions, 48 deletions
diff --git a/libbutl/optional.ixx b/libbutl/optional.ixx index 2c40d30..18c0b0b 100644 --- a/libbutl/optional.ixx +++ b/libbutl/optional.ixx @@ -55,24 +55,6 @@ namespace butl } template <typename T> - inline optional_data<T, false>:: - optional_data (const optional_data& o) - : v_ (o.v_) - { - if (v_) - new (&d_) T (o.d_); - } - - template <typename T> - inline optional_data<T, false>:: - optional_data (optional_data&& o) - : v_ (o.v_) - { - if (v_) - new (&d_) T (std::move (o.d_)); - } - - template <typename T> inline optional_data<T, false>& optional_data<T, false>:: operator= (const optional_data& o) { @@ -158,24 +140,6 @@ namespace butl } template <typename T> - inline optional_data<T, true>:: - optional_data (const optional_data& o) - : v_ (o.v_) - { - if (v_) - new (&d_) T (o.d_); - } - - template <typename T> - inline optional_data<T, true>:: - optional_data (optional_data&& o) - : v_ (o.v_) - { - if (v_) - new (&d_) T (std::move (o.d_)); - } - - template <typename T> inline optional_data<T, true>& optional_data<T, true>:: operator= (const optional_data& o) { diff --git a/libbutl/optional.mxx b/libbutl/optional.mxx index ae72399..9140432 100644 --- a/libbutl/optional.mxx +++ b/libbutl/optional.mxx @@ -57,20 +57,23 @@ LIBBUTL_MODEXPORT namespace butl constexpr optional_data (nullopt_t): e_ (), v_ (false) {} constexpr optional_data (const T& v): d_ (v), v_ (true) {} constexpr optional_data (T&& v): d_ (std::move (v)), v_ (true) {} + + constexpr optional_data (const optional_data& o): v_ (o.v_) {if (v_) new (&d_) T (o.d_);} + constexpr optional_data (optional_data&& o): v_ (o.v_) {if (v_) new (&d_) T (std::move (o.d_));} #else optional_data (): e_ (), v_ (false) {} optional_data (nullopt_t): e_ (), v_ (false) {} optional_data (const T& v): d_ (v), v_ (true) {} optional_data (T&& v): d_ (std::move (v)), v_ (true) {} + + optional_data (const optional_data& o): v_ (o.v_) {if (v_) new (&d_) T (o.d_);} + optional_data (optional_data&& o): v_ (o.v_) {if (v_) new (&d_) T (std::move (o.d_));} #endif optional_data& operator= (nullopt_t); optional_data& operator= (const T&); optional_data& operator= (T&&); - optional_data (const optional_data&); - optional_data (optional_data&&); - optional_data& operator= (const optional_data&); optional_data& operator= (optional_data&&); @@ -94,20 +97,23 @@ LIBBUTL_MODEXPORT namespace butl constexpr optional_data (nullopt_t): e_ (), v_ (false) {} constexpr optional_data (const T& v): d_ (v), v_ (true) {} constexpr optional_data (T&& v): d_ (std::move (v)), v_ (true) {} + + constexpr optional_data (const optional_data& o): v_ (o.v_) {if (v_) new (&d_) T (o.d_);} + constexpr optional_data (optional_data&& o): v_ (o.v_) {if (v_) new (&d_) T (std::move (o.d_));} #else optional_data (): e_ (), v_ (false) {} optional_data (nullopt_t): e_ (), v_ (false) {} optional_data (const T& v): d_ (v), v_ (true) {} optional_data (T&& v): d_ (std::move (v)), v_ (true) {} + + optional_data (const optional_data& o): v_ (o.v_) {if (v_) new (&d_) T (o.d_);} + optional_data (optional_data&& o): v_ (o.v_) {if (v_) new (&d_) T (std::move (o.d_));} #endif optional_data& operator= (nullopt_t); optional_data& operator= (const T&); optional_data& operator= (T&&); - optional_data (const optional_data&); - optional_data (optional_data&&); - optional_data& operator= (const optional_data&); optional_data& operator= (optional_data&&); }; @@ -126,17 +132,26 @@ LIBBUTL_MODEXPORT namespace butl constexpr optional (nullopt_t) {} constexpr optional (const T& v): base (v) {} constexpr optional (T&& v): base (std::move (v)) {} + + constexpr optional (const optional&) = default; + constexpr optional (optional&&) = default; #else optional () {} optional (nullopt_t) {} optional (const T& v): base (v) {} optional (T&& v): base (std::move (v)) {} + + optional (const optional&) = default; + optional (optional&&) = default; #endif optional& operator= (nullopt_t v) {static_cast<base&> (*this) = v; return *this;} optional& operator= (const T& v) {static_cast<base&> (*this) = v; return *this;} optional& operator= (T&& v) {static_cast<base&> (*this) = std::move (v); return *this;} + optional& operator= (const optional&) = default; + optional& operator= (optional&&) = default; + T& value () {return this->d_;} const T& value () const {return this->d_;} @@ -148,12 +163,6 @@ LIBBUTL_MODEXPORT namespace butl bool has_value () const {return this->v_;} explicit operator bool () const {return this->v_;} - - optional (const optional&) = default; - optional (optional&&) = default; - - optional& operator= (const optional&) = default; - optional& operator= (optional&&) = default; }; template <typename T> |