12
12
#define NONSTD_OPTIONAL_LITE_HPP
13
13
14
14
#define optional_lite_MAJOR 3
15
- #define optional_lite_MINOR 5
15
+ #define optional_lite_MINOR 6
16
16
#define optional_lite_PATCH 0
17
17
18
18
#define optional_lite_VERSION optional_STRINGIFY (optional_lite_MAJOR) "." optional_STRINGIFY(optional_lite_MINOR) "." optional_STRINGIFY(optional_lite_PATCH)
44
44
# define optional_CONFIG_SELECT_OPTIONAL ( optional_HAVE_STD_OPTIONAL ? optional_OPTIONAL_STD : optional_OPTIONAL_NONSTD )
45
45
#endif
46
46
47
+ // Control presence of extensions:
48
+
49
+ #ifndef optional_CONFIG_NO_EXTENSIONS
50
+ #define optional_CONFIG_NO_EXTENSIONS 0
51
+ #endif
52
+
47
53
// Control presence of exception handling (try and auto discover):
48
54
49
55
#ifndef optional_CONFIG_NO_EXCEPTIONS
50
56
# if defined(_MSC_VER)
51
57
# include < cstddef> // for _HAS_EXCEPTIONS
52
58
# endif
53
- # if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || (_HAS_EXCEPTIONS)
59
+ # if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || (defined( _HAS_EXCEPTIONS) && (_HAS_EXCEPTIONS) )
54
60
# define optional_CONFIG_NO_EXCEPTIONS 0
55
61
# else
56
62
# define optional_CONFIG_NO_EXCEPTIONS 1
57
63
# endif
58
64
#endif
59
65
60
- // C++ language version detection (C++20 is speculative):
66
+ // C++ language version detection (C++23 is speculative):
61
67
// Note: VC14.0/1900 (VS2015) lacks too much from C++14.
62
68
63
69
#ifndef optional_CPLUSPLUS
73
79
#define optional_CPP11_OR_GREATER_ ( optional_CPLUSPLUS >= 201103L )
74
80
#define optional_CPP14_OR_GREATER ( optional_CPLUSPLUS >= 201402L )
75
81
#define optional_CPP17_OR_GREATER ( optional_CPLUSPLUS >= 201703L )
76
- #define optional_CPP20_OR_GREATER ( optional_CPLUSPLUS >= 202000L )
82
+ #define optional_CPP20_OR_GREATER ( optional_CPLUSPLUS >= 202002L )
83
+ #define optional_CPP23_OR_GREATER ( optional_CPLUSPLUS >= 202300L )
77
84
78
85
// C++ language version (represent 98 as 3):
79
86
@@ -782,7 +789,7 @@ union storage_t
782
789
783
790
void construct_value ( value_type && v )
784
791
{
785
- ::new ( value_ptr () ) value_type ( std::move ( v ) );
792
+ ::new ( const_cast < void *>( static_cast < const volatile void *>( value_ptr ()) ) ) value_type ( std::move ( v ) );
786
793
}
787
794
788
795
template < class ... Args >
@@ -794,13 +801,13 @@ union storage_t
794
801
template < class ... Args >
795
802
void emplace ( Args&&... args )
796
803
{
797
- ::new ( value_ptr () ) value_type ( std::forward<Args>(args)... );
804
+ ::new ( const_cast < void *>( static_cast < const volatile void *>( value_ptr ()) ) ) value_type ( std::forward<Args>(args)... );
798
805
}
799
806
800
807
template < class U , class ... Args >
801
808
void emplace ( std::initializer_list<U> il, Args&&... args )
802
809
{
803
- ::new ( value_ptr () ) value_type ( il, std::forward<Args>(args)... );
810
+ ::new ( const_cast < void *>( static_cast < const volatile void *>( value_ptr ()) ) ) value_type ( il, std::forward<Args>(args)... );
804
811
}
805
812
806
813
#endif
@@ -1485,7 +1492,40 @@ class optional
1485
1492
return has_value () ? contained.value () : static_cast <value_type>( v );
1486
1493
}
1487
1494
1488
- #endif // optional_CPP11_OR_GREATER
1495
+ #endif // optional_HAVE( REF_QUALIFIER )
1496
+
1497
+ #if !optional_CONFIG_NO_EXTENSIONS
1498
+ #if optional_HAVE( REF_QUALIFIER )
1499
+
1500
+ template < typename F >
1501
+ optional_constexpr value_type value_or_eval ( F f ) const &
1502
+ {
1503
+ return has_value () ? contained.value () : f ();
1504
+ }
1505
+
1506
+ template < typename F >
1507
+ optional_constexpr14 value_type value_or_eval ( F f ) &&
1508
+ {
1509
+ if ( has_value () )
1510
+ {
1511
+ return std::move ( contained.value () );
1512
+ }
1513
+ else
1514
+ {
1515
+ return f ();
1516
+ }
1517
+ }
1518
+
1519
+ #else
1520
+
1521
+ template < typename F >
1522
+ optional_constexpr value_type value_or_eval ( F f ) const
1523
+ {
1524
+ return has_value () ? contained.value () : f ();
1525
+ }
1526
+
1527
+ #endif // optional_HAVE( REF_QUALIFIER )
1528
+ #endif // !optional_CONFIG_NO_EXTENSIONS
1489
1529
1490
1530
// x.x.3.6, modifiers
1491
1531
@@ -1515,7 +1555,7 @@ class optional
1515
1555
void initialize ( V && value )
1516
1556
{
1517
1557
assert ( ! has_value () );
1518
- contained.construct_value ( std::move ( value ) );
1558
+ contained.construct_value ( std::forward<V> ( value ) );
1519
1559
has_value_ = true ;
1520
1560
}
1521
1561
@@ -1530,185 +1570,185 @@ class optional
1530
1570
// Relational operators
1531
1571
1532
1572
template < typename T, typename U >
1533
- inline optional_constexpr bool operator ==( optional<T> const & x, optional<U> const & y )
1573
+ optional_nodiscard optional_constexpr bool operator ==( optional<T> const & x, optional<U> const & y )
1534
1574
{
1535
1575
return bool (x) != bool (y) ? false : !bool ( x ) ? true : *x == *y;
1536
1576
}
1537
1577
1538
1578
template < typename T, typename U >
1539
- inline optional_constexpr bool operator !=( optional<T> const & x, optional<U> const & y )
1579
+ optional_nodiscard optional_constexpr bool operator !=( optional<T> const & x, optional<U> const & y )
1540
1580
{
1541
1581
return !(x == y);
1542
1582
}
1543
1583
1544
1584
template < typename T, typename U >
1545
- inline optional_constexpr bool operator <( optional<T> const & x, optional<U> const & y )
1585
+ optional_nodiscard optional_constexpr bool operator <( optional<T> const & x, optional<U> const & y )
1546
1586
{
1547
1587
return (!y) ? false : (!x) ? true : *x < *y;
1548
1588
}
1549
1589
1550
1590
template < typename T, typename U >
1551
- inline optional_constexpr bool operator >( optional<T> const & x, optional<U> const & y )
1591
+ optional_nodiscard optional_constexpr bool operator >( optional<T> const & x, optional<U> const & y )
1552
1592
{
1553
1593
return (y < x);
1554
1594
}
1555
1595
1556
1596
template < typename T, typename U >
1557
- inline optional_constexpr bool operator <=( optional<T> const & x, optional<U> const & y )
1597
+ optional_nodiscard optional_constexpr bool operator <=( optional<T> const & x, optional<U> const & y )
1558
1598
{
1559
1599
return !(y < x);
1560
1600
}
1561
1601
1562
1602
template < typename T, typename U >
1563
- inline optional_constexpr bool operator >=( optional<T> const & x, optional<U> const & y )
1603
+ optional_nodiscard optional_constexpr bool operator >=( optional<T> const & x, optional<U> const & y )
1564
1604
{
1565
1605
return !(x < y);
1566
1606
}
1567
1607
1568
1608
// Comparison with nullopt
1569
1609
1570
1610
template < typename T >
1571
- inline optional_constexpr bool operator ==( optional<T> const & x, nullopt_t /* unused*/ ) optional_noexcept
1611
+ optional_nodiscard optional_constexpr bool operator ==( optional<T> const & x, nullopt_t /* unused*/ ) optional_noexcept
1572
1612
{
1573
1613
return (!x);
1574
1614
}
1575
1615
1576
1616
template < typename T >
1577
- inline optional_constexpr bool operator ==( nullopt_t /* unused*/ , optional<T> const & x ) optional_noexcept
1617
+ optional_nodiscard optional_constexpr bool operator ==( nullopt_t /* unused*/ , optional<T> const & x ) optional_noexcept
1578
1618
{
1579
1619
return (!x);
1580
1620
}
1581
1621
1582
1622
template < typename T >
1583
- inline optional_constexpr bool operator !=( optional<T> const & x, nullopt_t /* unused*/ ) optional_noexcept
1623
+ optional_nodiscard optional_constexpr bool operator !=( optional<T> const & x, nullopt_t /* unused*/ ) optional_noexcept
1584
1624
{
1585
1625
return bool (x);
1586
1626
}
1587
1627
1588
1628
template < typename T >
1589
- inline optional_constexpr bool operator !=( nullopt_t /* unused*/ , optional<T> const & x ) optional_noexcept
1629
+ optional_nodiscard optional_constexpr bool operator !=( nullopt_t /* unused*/ , optional<T> const & x ) optional_noexcept
1590
1630
{
1591
1631
return bool (x);
1592
1632
}
1593
1633
1594
1634
template < typename T >
1595
- inline optional_constexpr bool operator <( optional<T> const & /* unused*/ , nullopt_t /* unused*/ ) optional_noexcept
1635
+ optional_nodiscard optional_constexpr bool operator <( optional<T> const & /* unused*/ , nullopt_t /* unused*/ ) optional_noexcept
1596
1636
{
1597
1637
return false ;
1598
1638
}
1599
1639
1600
1640
template < typename T >
1601
- inline optional_constexpr bool operator <( nullopt_t /* unused*/ , optional<T> const & x ) optional_noexcept
1641
+ optional_nodiscard optional_constexpr bool operator <( nullopt_t /* unused*/ , optional<T> const & x ) optional_noexcept
1602
1642
{
1603
1643
return bool (x);
1604
1644
}
1605
1645
1606
1646
template < typename T >
1607
- inline optional_constexpr bool operator <=( optional<T> const & x, nullopt_t /* unused*/ ) optional_noexcept
1647
+ optional_nodiscard optional_constexpr bool operator <=( optional<T> const & x, nullopt_t /* unused*/ ) optional_noexcept
1608
1648
{
1609
1649
return (!x);
1610
1650
}
1611
1651
1612
1652
template < typename T >
1613
- inline optional_constexpr bool operator <=( nullopt_t /* unused*/ , optional<T> const & /* unused*/ ) optional_noexcept
1653
+ optional_nodiscard optional_constexpr bool operator <=( nullopt_t /* unused*/ , optional<T> const & /* unused*/ ) optional_noexcept
1614
1654
{
1615
1655
return true ;
1616
1656
}
1617
1657
1618
1658
template < typename T >
1619
- inline optional_constexpr bool operator >( optional<T> const & x, nullopt_t /* unused*/ ) optional_noexcept
1659
+ optional_nodiscard optional_constexpr bool operator >( optional<T> const & x, nullopt_t /* unused*/ ) optional_noexcept
1620
1660
{
1621
1661
return bool (x);
1622
1662
}
1623
1663
1624
1664
template < typename T >
1625
- inline optional_constexpr bool operator >( nullopt_t /* unused*/ , optional<T> const & /* unused*/ ) optional_noexcept
1665
+ optional_nodiscard optional_constexpr bool operator >( nullopt_t /* unused*/ , optional<T> const & /* unused*/ ) optional_noexcept
1626
1666
{
1627
1667
return false ;
1628
1668
}
1629
1669
1630
1670
template < typename T >
1631
- inline optional_constexpr bool operator >=( optional<T> const & /* unused*/ , nullopt_t /* unused*/ ) optional_noexcept
1671
+ optional_nodiscard optional_constexpr bool operator >=( optional<T> const & /* unused*/ , nullopt_t /* unused*/ ) optional_noexcept
1632
1672
{
1633
1673
return true ;
1634
1674
}
1635
1675
1636
1676
template < typename T >
1637
- inline optional_constexpr bool operator >=( nullopt_t /* unused*/ , optional<T> const & x ) optional_noexcept
1677
+ optional_nodiscard optional_constexpr bool operator >=( nullopt_t /* unused*/ , optional<T> const & x ) optional_noexcept
1638
1678
{
1639
1679
return (!x);
1640
1680
}
1641
1681
1642
1682
// Comparison with T
1643
1683
1644
1684
template < typename T, typename U >
1645
- inline optional_constexpr bool operator ==( optional<T> const & x, U const & v )
1685
+ optional_nodiscard optional_constexpr bool operator ==( optional<T> const & x, U const & v )
1646
1686
{
1647
1687
return bool (x) ? *x == v : false ;
1648
1688
}
1649
1689
1650
1690
template < typename T, typename U >
1651
- inline optional_constexpr bool operator ==( U const & v, optional<T> const & x )
1691
+ optional_nodiscard optional_constexpr bool operator ==( U const & v, optional<T> const & x )
1652
1692
{
1653
1693
return bool (x) ? v == *x : false ;
1654
1694
}
1655
1695
1656
1696
template < typename T, typename U >
1657
- inline optional_constexpr bool operator !=( optional<T> const & x, U const & v )
1697
+ optional_nodiscard optional_constexpr bool operator !=( optional<T> const & x, U const & v )
1658
1698
{
1659
1699
return bool (x) ? *x != v : true ;
1660
1700
}
1661
1701
1662
1702
template < typename T, typename U >
1663
- inline optional_constexpr bool operator !=( U const & v, optional<T> const & x )
1703
+ optional_nodiscard optional_constexpr bool operator !=( U const & v, optional<T> const & x )
1664
1704
{
1665
1705
return bool (x) ? v != *x : true ;
1666
1706
}
1667
1707
1668
1708
template < typename T, typename U >
1669
- inline optional_constexpr bool operator <( optional<T> const & x, U const & v )
1709
+ optional_nodiscard optional_constexpr bool operator <( optional<T> const & x, U const & v )
1670
1710
{
1671
1711
return bool (x) ? *x < v : true ;
1672
1712
}
1673
1713
1674
1714
template < typename T, typename U >
1675
- inline optional_constexpr bool operator <( U const & v, optional<T> const & x )
1715
+ optional_nodiscard optional_constexpr bool operator <( U const & v, optional<T> const & x )
1676
1716
{
1677
1717
return bool (x) ? v < *x : false ;
1678
1718
}
1679
1719
1680
1720
template < typename T, typename U >
1681
- inline optional_constexpr bool operator <=( optional<T> const & x, U const & v )
1721
+ optional_nodiscard optional_constexpr bool operator <=( optional<T> const & x, U const & v )
1682
1722
{
1683
1723
return bool (x) ? *x <= v : true ;
1684
1724
}
1685
1725
1686
1726
template < typename T, typename U >
1687
- inline optional_constexpr bool operator <=( U const & v, optional<T> const & x )
1727
+ optional_nodiscard optional_constexpr bool operator <=( U const & v, optional<T> const & x )
1688
1728
{
1689
1729
return bool (x) ? v <= *x : false ;
1690
1730
}
1691
1731
1692
1732
template < typename T, typename U >
1693
- inline optional_constexpr bool operator >( optional<T> const & x, U const & v )
1733
+ optional_nodiscard optional_constexpr bool operator >( optional<T> const & x, U const & v )
1694
1734
{
1695
1735
return bool (x) ? *x > v : false ;
1696
1736
}
1697
1737
1698
1738
template < typename T, typename U >
1699
- inline optional_constexpr bool operator >( U const & v, optional<T> const & x )
1739
+ optional_nodiscard optional_constexpr bool operator >( U const & v, optional<T> const & x )
1700
1740
{
1701
1741
return bool (x) ? v > *x : true ;
1702
1742
}
1703
1743
1704
1744
template < typename T, typename U >
1705
- inline optional_constexpr bool operator >=( optional<T> const & x, U const & v )
1745
+ optional_nodiscard optional_constexpr bool operator >=( optional<T> const & x, U const & v )
1706
1746
{
1707
1747
return bool (x) ? *x >= v : false ;
1708
1748
}
1709
1749
1710
1750
template < typename T, typename U >
1711
- inline optional_constexpr bool operator >=( U const & v, optional<T> const & x )
1751
+ optional_nodiscard optional_constexpr bool operator >=( U const & v, optional<T> const & x )
1712
1752
{
1713
1753
return bool (x) ? v >= *x : true ;
1714
1754
}
0 commit comments