Merge pull request #55 from tonyelewis/add-constexpr-support

Add constexpr to operators (w/ basic docs, tests)
This commit is contained in:
Daniel Frey authored and GitHub committed 2020-04-12 13:07:24 +02:00
commit bdc5b5cf3a
3 files changed
+146 -33

No files matched your search

+34 -8
View File
@@ -50,9 +50,9 @@ namespace
void operator!() const;
public:
convertible_to_bool( const bool value ) : _value( value ) {}
BOOST_OPS_CONSTEXPR convertible_to_bool( const bool value ) : _value( value ) {}
operator unspecified_bool_type() const
BOOST_OPS_CONSTEXPR operator unspecified_bool_type() const
{ return _value ? &convertible_to_bool::_value : 0; }
};
@@ -64,12 +64,12 @@ namespace
, boost::shiftable<Wrapped1<T> >
{
public:
explicit Wrapped1( T v = T() ) : _value(v) {}
T value() const { return _value; }
BOOST_OPS_CONSTEXPR explicit Wrapped1( T v = T() ) : _value(v) {}
BOOST_OPS_CONSTEXPR T value() const { return _value; }
convertible_to_bool operator<(const Wrapped1& x) const
BOOST_OPS_CONSTEXPR convertible_to_bool operator<(const Wrapped1& x) const
{ return _value < x._value; }
convertible_to_bool operator==(const Wrapped1& x) const
BOOST_OPS_CONSTEXPR convertible_to_bool operator==(const Wrapped1& x) const
{ return _value == x._value; }
Wrapped1& operator+=(const Wrapped1& x)
@@ -92,8 +92,8 @@ namespace
{ _value <<= x._value; return *this; }
Wrapped1& operator>>=(const Wrapped1& x)
{ _value >>= x._value; return *this; }
Wrapped1& operator++() { ++_value; return *this; }
Wrapped1& operator--() { --_value; return *this; }
Wrapped1& operator++() { ++_value; return *this; }
Wrapped1& operator--() { --_value; return *this; }
private:
T _value;
@@ -932,5 +932,31 @@ main()
cout << "Performed tests on MyLongInt objects.\n";
// Where C++11 constexpr is available, compile-time test some of the operators that are able to use it to propagate constexpr
#ifndef BOOST_NO_CXX11_CONSTEXPR
#if !defined(BOOST_MSVC) || (_MSC_VER >= 1922)
static_assert( ! static_cast<bool>( MyInt{ 1 } == MyInt{ 2 } ), "" );
static_assert( MyInt{ 1 } != MyInt{ 2 }, "" );
static_assert( MyInt{ 1 } < MyInt{ 2 }, "" );
static_assert( MyInt{ 1 } <= MyInt{ 2 }, "" );
static_assert( ! static_cast<bool>( MyInt{ 1 } > MyInt{ 2 } ), "" );
static_assert( ! static_cast<bool>( MyInt{ 1 } >= MyInt{ 2 } ), "" );
static_assert( ! static_cast<bool>( MyInt{ 2 } == MyInt{ 1 } ), "" );
static_assert( MyInt{ 2 } != MyInt{ 1 }, "" );
static_assert( ! static_cast<bool>( MyInt{ 2 } < MyInt{ 1 } ), "" );
static_assert( ! static_cast<bool>( MyInt{ 2 } <= MyInt{ 1 } ), "" );
static_assert( MyInt{ 2 } > MyInt{ 1 }, "" );
static_assert( MyInt{ 2 } >= MyInt{ 1 }, "" );
static_assert( MyInt{ 1 } == MyInt{ 1 }, "" );
static_assert( ! static_cast<bool>( MyInt{ 1 } != MyInt{ 1 } ), "" );
static_assert( ! static_cast<bool>( MyInt{ 1 } < MyInt{ 1 } ), "" );
static_assert( MyInt{ 1 } <= MyInt{ 1 }, "" );
static_assert( ! static_cast<bool>( MyInt{ 1 } > MyInt{ 1 } ), "" );
static_assert( MyInt{ 1 } >= MyInt{ 1 }, "" );
#endif
#endif
return boost::report_errors();
}