@@ -490,8 +490,6 @@ struct endline
490
490
void operator ()() const {
491
491
os << std::endl;
492
492
}
493
- private:
494
- endline& operator =(const endline&) RXCPP_DELETE;
495
493
};
496
494
497
495
template <class OStream , class ValueType >
@@ -503,8 +501,6 @@ struct insert_value
503
501
void operator ()() const {
504
502
os << value;
505
503
}
506
- private:
507
- insert_value& operator =(const insert_value&) RXCPP_DELETE;
508
504
};
509
505
510
506
template <class OStream , class Function >
@@ -516,8 +512,6 @@ struct insert_function
516
512
void operator ()() const {
517
513
call (os);
518
514
}
519
- private:
520
- insert_function& operator =(const insert_function&) RXCPP_DELETE;
521
515
};
522
516
523
517
template <class OStream , class Delimit >
@@ -568,131 +562,94 @@ namespace detail {
568
562
template <class T >
569
563
class maybe
570
564
{
571
- bool is_set;
572
- typename std::aligned_storage<sizeof (T), std::alignment_of<T>::value>::type
573
- storage;
565
+ std::optional<T> val_;
574
566
public:
575
- maybe ()
576
- : is_set(false )
577
- {
578
- }
567
+ maybe () = default ;
579
568
580
569
maybe (const T& value)
581
- : is_set( false )
570
+ : val_(value )
582
571
{
583
- new (reinterpret_cast <T*>(&storage)) T (value);
584
- is_set = true ;
585
572
}
586
573
587
574
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))
604
576
{
605
- if (other.is_set ) {
606
- new (reinterpret_cast <T*>(&storage)) T (std::move (other.get ()));
607
- is_set = true ;
608
- other.reset ();
609
- }
610
577
}
611
578
612
- ~maybe ()
613
- {
614
- reset ();
615
- }
579
+ maybe (const maybe& other) = default ;
580
+ maybe (maybe&& other) = default ;
616
581
617
582
using value_type = T;
618
583
using iterator = T *;
619
584
using const_iterator = const T *;
620
585
621
586
bool empty () const {
622
- return !is_set ;
587
+ return !val_ ;
623
588
}
624
589
625
590
std::size_t size () const {
626
- return is_set ? 1 : 0 ;
591
+ return val_ ? 1 : 0 ;
627
592
}
628
593
629
594
iterator begin () {
630
- return reinterpret_cast <T*>(&storage) ;
595
+ return val_ ? &*val_ : nullptr ;
631
596
}
632
597
const_iterator begin () const {
633
- return reinterpret_cast <T*>(&storage) ;
598
+ return val_ ? &*val_ : nullptr ;
634
599
}
635
600
636
601
iterator end () {
637
- return reinterpret_cast <T*>(&storage ) + size ();
602
+ return begin ( ) + size ();
638
603
}
639
604
const_iterator end () const {
640
- return reinterpret_cast <T*>(&storage ) + size ();
605
+ return begin ( ) + size ();
641
606
}
642
607
643
608
T* operator ->() {
644
- if (!is_set ) std::terminate ();
645
- return reinterpret_cast <T*>(&storage );
609
+ if (!val_ ) std::terminate ();
610
+ return val_. operator ->( );
646
611
}
647
612
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 ->( );
650
615
}
651
616
652
617
T& operator *() {
653
- if (!is_set ) std::terminate ();
654
- return *reinterpret_cast <T*>(&storage) ;
618
+ if (!val_ ) std::terminate ();
619
+ return *val_ ;
655
620
}
656
621
const T& operator *() const {
657
- if (!is_set ) std::terminate ();
658
- return *reinterpret_cast <T*>(&storage) ;
622
+ if (!val_ ) std::terminate ();
623
+ return *val_ ;
659
624
}
660
625
661
626
T& get () {
662
- if (!is_set) std::terminate ();
663
- return *reinterpret_cast <T*>(&storage);
627
+ return **this ;
664
628
}
665
629
const T& get () const {
666
- if (!is_set) std::terminate ();
667
- return *reinterpret_cast <const T*>(&storage);
630
+ return **this ;
668
631
}
669
632
670
633
void reset ()
671
634
{
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 ();
677
636
}
678
637
679
- template <class U >
638
+ template <class U = T >
680
639
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);
684
641
}
685
642
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;
688
648
return *this ;
689
649
}
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);
696
653
return *this ;
697
654
}
698
655
};
0 commit comments