@@ -490,8 +490,6 @@ struct endline
490490 void operator ()() const {
491491 os << std::endl;
492492 }
493- private:
494- endline& operator =(const endline&) RXCPP_DELETE;
495493};
496494
497495template <class OStream , class ValueType >
@@ -503,8 +501,6 @@ struct insert_value
503501 void operator ()() const {
504502 os << value;
505503 }
506- private:
507- insert_value& operator =(const insert_value&) RXCPP_DELETE;
508504};
509505
510506template <class OStream , class Function >
@@ -516,8 +512,6 @@ struct insert_function
516512 void operator ()() const {
517513 call (os);
518514 }
519- private:
520- insert_function& operator =(const insert_function&) RXCPP_DELETE;
521515};
522516
523517template <class OStream , class Delimit >
@@ -568,131 +562,94 @@ namespace detail {
568562template <class T >
569563class maybe
570564{
571- bool is_set;
572- typename std::aligned_storage<sizeof (T), std::alignment_of<T>::value>::type
573- storage;
565+ std::optional<T> val_;
574566public:
575- maybe ()
576- : is_set(false )
577- {
578- }
567+ maybe () = default ;
579568
580569 maybe (const T& value)
581- : is_set( false )
570+ : val_(value )
582571 {
583- new (reinterpret_cast <T*>(&storage)) T (value);
584- is_set = true ;
585572 }
586573
587574 maybe (T&& value)
588- : is_set(false )
589- {
590- new (reinterpret_cast <T*>(&storage)) T (std::move (value));
591- is_set = true ;
592- }
593-
594- maybe (const maybe& other)
595- : is_set(false )
596- {
597- if (other.is_set ) {
598- new (reinterpret_cast <T*>(&storage)) T (other.get ());
599- is_set = true ;
600- }
601- }
602- maybe (maybe&& other)
603- : is_set(false )
575+ : val_(std::move(value))
604576 {
605- if (other.is_set ) {
606- new (reinterpret_cast <T*>(&storage)) T (std::move (other.get ()));
607- is_set = true ;
608- other.reset ();
609- }
610577 }
611578
612- ~maybe ()
613- {
614- reset ();
615- }
579+ maybe (const maybe& other) = default ;
580+ maybe (maybe&& other) = default ;
616581
617582 using value_type = T;
618583 using iterator = T *;
619584 using const_iterator = const T *;
620585
621586 bool empty () const {
622- return !is_set ;
587+ return !val_ ;
623588 }
624589
625590 std::size_t size () const {
626- return is_set ? 1 : 0 ;
591+ return val_ ? 1 : 0 ;
627592 }
628593
629594 iterator begin () {
630- return reinterpret_cast <T*>(&storage) ;
595+ return val_ ? &*val_ : nullptr ;
631596 }
632597 const_iterator begin () const {
633- return reinterpret_cast <T*>(&storage) ;
598+ return val_ ? &*val_ : nullptr ;
634599 }
635600
636601 iterator end () {
637- return reinterpret_cast <T*>(&storage ) + size ();
602+ return begin ( ) + size ();
638603 }
639604 const_iterator end () const {
640- return reinterpret_cast <T*>(&storage ) + size ();
605+ return begin ( ) + size ();
641606 }
642607
643608 T* operator ->() {
644- if (!is_set ) std::terminate ();
645- return reinterpret_cast <T*>(&storage );
609+ if (!val_ ) std::terminate ();
610+ return val_. operator ->( );
646611 }
647612 const T* operator ->() const {
648- if (!is_set ) std::terminate ();
649- return reinterpret_cast <T*>(&storage );
613+ if (!val_ ) std::terminate ();
614+ return val_. operator ->( );
650615 }
651616
652617 T& operator *() {
653- if (!is_set ) std::terminate ();
654- return *reinterpret_cast <T*>(&storage) ;
618+ if (!val_ ) std::terminate ();
619+ return *val_ ;
655620 }
656621 const T& operator *() const {
657- if (!is_set ) std::terminate ();
658- return *reinterpret_cast <T*>(&storage) ;
622+ if (!val_ ) std::terminate ();
623+ return *val_ ;
659624 }
660625
661626 T& get () {
662- if (!is_set) std::terminate ();
663- return *reinterpret_cast <T*>(&storage);
627+ return **this ;
664628 }
665629 const T& get () const {
666- if (!is_set) std::terminate ();
667- return *reinterpret_cast <const T*>(&storage);
630+ return **this ;
668631 }
669632
670633 void reset ()
671634 {
672- if (is_set) {
673- is_set = false ;
674- reinterpret_cast <T*>(&storage)->~T ();
675- // std::fill_n(reinterpret_cast<char*>(&storage), sizeof(T), 0);
676- }
635+ val_.reset ();
677636 }
678637
679- template <class U >
638+ template <class U = T >
680639 void reset (U&& value) {
681- reset ();
682- new (reinterpret_cast <T*>(&storage)) T (std::forward<U>(value));
683- is_set = true ;
640+ *this = std::forward<U>(value);
684641 }
685642
686- maybe& operator =(const T& other) {
687- reset (other);
643+ maybe& operator =(const maybe& other) = default ;
644+ maybe& operator =(maybe&& other) = default ;
645+
646+ maybe& operator =(const T& value) {
647+ val_ = value;
688648 return *this ;
689649 }
690- maybe& operator =(const maybe& other) {
691- if (!other.empty ()) {
692- reset (other.get ());
693- } else {
694- reset ();
695- }
650+
651+ maybe& operator =(T&& value) {
652+ val_ = std::move (value);
696653 return *this ;
697654 }
698655};
0 commit comments