mirror of
https://github.com/boostorg/logic.git
synced 2025-07-31 04:37:14 +02:00
C++11 updates for tribool: added noexcepts and constexprs, fixed warnings
This commit is contained in:
@ -14,7 +14,7 @@
|
|||||||
#include <boost/config.hpp>
|
#include <boost/config.hpp>
|
||||||
#include <boost/detail/workaround.hpp>
|
#include <boost/detail/workaround.hpp>
|
||||||
|
|
||||||
#if defined(_MSC_VER)
|
#ifdef BOOST_HAS_PRAGMA_ONCE
|
||||||
# pragma once
|
# pragma once
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -59,9 +59,9 @@ typedef bool (*indeterminate_keyword_t)(tribool, detail::indeterminate_t);
|
|||||||
* \returns <tt>x.value == tribool::indeterminate_value</tt>
|
* \returns <tt>x.value == tribool::indeterminate_value</tt>
|
||||||
* \throws nothrow
|
* \throws nothrow
|
||||||
*/
|
*/
|
||||||
inline bool
|
BOOST_CONSTEXPR inline bool
|
||||||
indeterminate(tribool x,
|
indeterminate(tribool x,
|
||||||
detail::indeterminate_t dummy = detail::indeterminate_t());
|
detail::indeterminate_t dummy = detail::indeterminate_t()) BOOST_NOEXCEPT;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief A 3-state boolean type.
|
* \brief A 3-state boolean type.
|
||||||
@ -85,7 +85,7 @@ public:
|
|||||||
*
|
*
|
||||||
* \throws nothrow
|
* \throws nothrow
|
||||||
*/
|
*/
|
||||||
tribool() : value(false_value) {}
|
BOOST_CONSTEXPR tribool() BOOST_NOEXCEPT : value(false_value) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new 3-state boolean value with the given boolean
|
* Construct a new 3-state boolean value with the given boolean
|
||||||
@ -93,14 +93,14 @@ public:
|
|||||||
*
|
*
|
||||||
* \throws nothrow
|
* \throws nothrow
|
||||||
*/
|
*/
|
||||||
tribool(bool initial_value) : value(initial_value? true_value : false_value) {}
|
BOOST_CONSTEXPR tribool(bool initial_value) BOOST_NOEXCEPT : value(initial_value? true_value : false_value) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new 3-state boolean value with an indeterminate value.
|
* Construct a new 3-state boolean value with an indeterminate value.
|
||||||
*
|
*
|
||||||
* \throws nothrow
|
* \throws nothrow
|
||||||
*/
|
*/
|
||||||
tribool(indeterminate_keyword_t) : value(indeterminate_value) {}
|
BOOST_CONSTEXPR tribool(indeterminate_keyword_t) BOOST_NOEXCEPT : value(indeterminate_value) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use a 3-state boolean in a boolean context. Will evaluate true in a
|
* Use a 3-state boolean in a boolean context. Will evaluate true in a
|
||||||
@ -109,7 +109,7 @@ public:
|
|||||||
* \returns true if the 3-state boolean is true, false otherwise
|
* \returns true if the 3-state boolean is true, false otherwise
|
||||||
* \throws nothrow
|
* \throws nothrow
|
||||||
*/
|
*/
|
||||||
operator safe_bool() const
|
BOOST_CONSTEXPR operator safe_bool() const BOOST_NOEXCEPT
|
||||||
{
|
{
|
||||||
return value == true_value? &dummy::nonnull : 0;
|
return value == true_value? &dummy::nonnull : 0;
|
||||||
}
|
}
|
||||||
@ -123,7 +123,7 @@ public:
|
|||||||
|
|
||||||
// Check if the given tribool has an indeterminate value. Also doubles as a
|
// Check if the given tribool has an indeterminate value. Also doubles as a
|
||||||
// keyword for the 'indeterminate' value
|
// keyword for the 'indeterminate' value
|
||||||
inline bool indeterminate(tribool x, detail::indeterminate_t)
|
BOOST_CONSTEXPR inline bool indeterminate(tribool x, detail::indeterminate_t) BOOST_NOEXCEPT
|
||||||
{
|
{
|
||||||
return x.value == tribool::indeterminate_value;
|
return x.value == tribool::indeterminate_value;
|
||||||
}
|
}
|
||||||
@ -156,7 +156,7 @@ inline bool indeterminate(tribool x, detail::indeterminate_t)
|
|||||||
* </table>
|
* </table>
|
||||||
* \throws nothrow
|
* \throws nothrow
|
||||||
*/
|
*/
|
||||||
inline tribool operator!(tribool x)
|
BOOST_CONSTEXPR inline tribool operator!(tribool x) BOOST_NOEXCEPT
|
||||||
{
|
{
|
||||||
return x.value == tribool::false_value? tribool(true)
|
return x.value == tribool::false_value? tribool(true)
|
||||||
:x.value == tribool::true_value? tribool(false)
|
:x.value == tribool::true_value? tribool(false)
|
||||||
@ -196,38 +196,36 @@ inline tribool operator!(tribool x)
|
|||||||
* </table>
|
* </table>
|
||||||
* \throws nothrow
|
* \throws nothrow
|
||||||
*/
|
*/
|
||||||
inline tribool operator&&(tribool x, tribool y)
|
BOOST_CONSTEXPR inline tribool operator&&(tribool x, tribool y) BOOST_NOEXCEPT
|
||||||
{
|
{
|
||||||
if (static_cast<bool>(!x) || static_cast<bool>(!y))
|
return (static_cast<bool>(!x) || static_cast<bool>(!y))
|
||||||
return false;
|
? tribool(false)
|
||||||
else if (static_cast<bool>(x) && static_cast<bool>(y))
|
: ((static_cast<bool>(x) && static_cast<bool>(y)) ? tribool(true) : indeterminate)
|
||||||
return true;
|
;
|
||||||
else
|
|
||||||
return indeterminate;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \overload
|
* \overload
|
||||||
*/
|
*/
|
||||||
inline tribool operator&&(tribool x, bool y)
|
BOOST_CONSTEXPR inline tribool operator&&(tribool x, bool y) BOOST_NOEXCEPT
|
||||||
{ return y? x : tribool(false); }
|
{ return y? x : tribool(false); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \overload
|
* \overload
|
||||||
*/
|
*/
|
||||||
inline tribool operator&&(bool x, tribool y)
|
BOOST_CONSTEXPR inline tribool operator&&(bool x, tribool y) BOOST_NOEXCEPT
|
||||||
{ return x? y : tribool(false); }
|
{ return x? y : tribool(false); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \overload
|
* \overload
|
||||||
*/
|
*/
|
||||||
inline tribool operator&&(indeterminate_keyword_t, tribool x)
|
BOOST_CONSTEXPR inline tribool operator&&(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT
|
||||||
{ return !x? tribool(false) : tribool(indeterminate); }
|
{ return !x? tribool(false) : tribool(indeterminate); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \overload
|
* \overload
|
||||||
*/
|
*/
|
||||||
inline tribool operator&&(tribool x, indeterminate_keyword_t)
|
BOOST_CONSTEXPR inline tribool operator&&(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT
|
||||||
{ return !x? tribool(false) : tribool(indeterminate); }
|
{ return !x? tribool(false) : tribool(indeterminate); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -263,38 +261,36 @@ inline tribool operator&&(tribool x, indeterminate_keyword_t)
|
|||||||
* </table>
|
* </table>
|
||||||
* \throws nothrow
|
* \throws nothrow
|
||||||
*/
|
*/
|
||||||
inline tribool operator||(tribool x, tribool y)
|
BOOST_CONSTEXPR inline tribool operator||(tribool x, tribool y) BOOST_NOEXCEPT
|
||||||
{
|
{
|
||||||
if (static_cast<bool>(!x) && static_cast<bool>(!y))
|
return (static_cast<bool>(!x) && static_cast<bool>(!y))
|
||||||
return false;
|
? tribool(false)
|
||||||
else if (static_cast<bool>(x) || static_cast<bool>(y))
|
: ((static_cast<bool>(x) || static_cast<bool>(y)) ? tribool(true) : tribool(indeterminate))
|
||||||
return true;
|
;
|
||||||
else
|
|
||||||
return indeterminate;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \overload
|
* \overload
|
||||||
*/
|
*/
|
||||||
inline tribool operator||(tribool x, bool y)
|
BOOST_CONSTEXPR inline tribool operator||(tribool x, bool y) BOOST_NOEXCEPT
|
||||||
{ return y? tribool(true) : x; }
|
{ return y? tribool(true) : x; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \overload
|
* \overload
|
||||||
*/
|
*/
|
||||||
inline tribool operator||(bool x, tribool y)
|
BOOST_CONSTEXPR inline tribool operator||(bool x, tribool y) BOOST_NOEXCEPT
|
||||||
{ return x? tribool(true) : y; }
|
{ return x? tribool(true) : y; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \overload
|
* \overload
|
||||||
*/
|
*/
|
||||||
inline tribool operator||(indeterminate_keyword_t, tribool x)
|
BOOST_CONSTEXPR inline tribool operator||(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT
|
||||||
{ return x? tribool(true) : tribool(indeterminate); }
|
{ return x? tribool(true) : tribool(indeterminate); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \overload
|
* \overload
|
||||||
*/
|
*/
|
||||||
inline tribool operator||(tribool x, indeterminate_keyword_t)
|
BOOST_CONSTEXPR inline tribool operator||(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT
|
||||||
{ return x? tribool(true) : tribool(indeterminate); }
|
{ return x? tribool(true) : tribool(indeterminate); }
|
||||||
//@}
|
//@}
|
||||||
|
|
||||||
@ -331,34 +327,34 @@ inline tribool operator||(tribool x, indeterminate_keyword_t)
|
|||||||
* </table>
|
* </table>
|
||||||
* \throws nothrow
|
* \throws nothrow
|
||||||
*/
|
*/
|
||||||
inline tribool operator==(tribool x, tribool y)
|
BOOST_CONSTEXPR inline tribool operator==(tribool x, tribool y) BOOST_NOEXCEPT
|
||||||
{
|
{
|
||||||
if (indeterminate(x) || indeterminate(y))
|
return (indeterminate(x) || indeterminate(y))
|
||||||
return indeterminate;
|
? indeterminate
|
||||||
else
|
: ((x && y) || (!x && !y))
|
||||||
return (x && y) || (!x && !y);
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \overload
|
* \overload
|
||||||
*/
|
*/
|
||||||
inline tribool operator==(tribool x, bool y) { return x == tribool(y); }
|
BOOST_CONSTEXPR inline tribool operator==(tribool x, bool y) BOOST_NOEXCEPT { return x == tribool(y); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \overload
|
* \overload
|
||||||
*/
|
*/
|
||||||
inline tribool operator==(bool x, tribool y) { return tribool(x) == y; }
|
BOOST_CONSTEXPR inline tribool operator==(bool x, tribool y) BOOST_NOEXCEPT { return tribool(x) == y; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \overload
|
* \overload
|
||||||
*/
|
*/
|
||||||
inline tribool operator==(indeterminate_keyword_t, tribool x)
|
BOOST_CONSTEXPR inline tribool operator==(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT
|
||||||
{ return tribool(indeterminate) == x; }
|
{ return tribool(indeterminate) == x; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \overload
|
* \overload
|
||||||
*/
|
*/
|
||||||
inline tribool operator==(tribool x, indeterminate_keyword_t)
|
BOOST_CONSTEXPR inline tribool operator==(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT
|
||||||
{ return tribool(indeterminate) == x; }
|
{ return tribool(indeterminate) == x; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -394,34 +390,34 @@ inline tribool operator==(tribool x, indeterminate_keyword_t)
|
|||||||
* </table>
|
* </table>
|
||||||
* \throws nothrow
|
* \throws nothrow
|
||||||
*/
|
*/
|
||||||
inline tribool operator!=(tribool x, tribool y)
|
BOOST_CONSTEXPR inline tribool operator!=(tribool x, tribool y) BOOST_NOEXCEPT
|
||||||
{
|
{
|
||||||
if (indeterminate(x) || indeterminate(y))
|
return (indeterminate(x) || indeterminate(y))
|
||||||
return indeterminate;
|
? indeterminate
|
||||||
else
|
: !((x && y) || (!x && !y))
|
||||||
return !((x && y) || (!x && !y));
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \overload
|
* \overload
|
||||||
*/
|
*/
|
||||||
inline tribool operator!=(tribool x, bool y) { return x != tribool(y); }
|
BOOST_CONSTEXPR inline tribool operator!=(tribool x, bool y) BOOST_NOEXCEPT { return x != tribool(y); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \overload
|
* \overload
|
||||||
*/
|
*/
|
||||||
inline tribool operator!=(bool x, tribool y) { return tribool(x) != y; }
|
BOOST_CONSTEXPR inline tribool operator!=(bool x, tribool y) BOOST_NOEXCEPT { return tribool(x) != y; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \overload
|
* \overload
|
||||||
*/
|
*/
|
||||||
inline tribool operator!=(indeterminate_keyword_t, tribool x)
|
BOOST_CONSTEXPR inline tribool operator!=(indeterminate_keyword_t, tribool x) BOOST_NOEXCEPT
|
||||||
{ return tribool(indeterminate) != x; }
|
{ return tribool(indeterminate) != x; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \overload
|
* \overload
|
||||||
*/
|
*/
|
||||||
inline tribool operator!=(tribool x, indeterminate_keyword_t)
|
BOOST_CONSTEXPR inline tribool operator!=(tribool x, indeterminate_keyword_t) BOOST_NOEXCEPT
|
||||||
{ return x != tribool(indeterminate); }
|
{ return x != tribool(indeterminate); }
|
||||||
|
|
||||||
} } // end namespace boost::logic
|
} } // end namespace boost::logic
|
||||||
|
@ -279,9 +279,9 @@ operator>>(std::basic_istream<CharT, Traits>& in, tribool& x)
|
|||||||
bool falsename_ok = true, truename_ok = true, othername_ok = true;
|
bool falsename_ok = true, truename_ok = true, othername_ok = true;
|
||||||
|
|
||||||
// Modeled after the code from Library DR 17
|
// Modeled after the code from Library DR 17
|
||||||
while (falsename_ok && pos < falsename.size()
|
while ((falsename_ok && pos < falsename.size())
|
||||||
|| truename_ok && pos < truename.size()
|
|| (truename_ok && pos < truename.size())
|
||||||
|| othername_ok && pos < othername.size()) {
|
|| (othername_ok && pos < othername.size())) {
|
||||||
typename Traits::int_type c = in.get();
|
typename Traits::int_type c = in.get();
|
||||||
if (c == Traits::eof())
|
if (c == Traits::eof())
|
||||||
return in;
|
return in;
|
||||||
|
@ -114,6 +114,24 @@ int test_main(int, char*[])
|
|||||||
BOOST_CHECK(false);
|
BOOST_CHECK(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef BOOST_NO_CXX11_CONSTEXPR
|
||||||
|
constexpr bool res_ors = indeterminate(false || tribool(false) || false || indeterminate); // true
|
||||||
|
BOOST_CHECK(res_ors);
|
||||||
|
char array_ors[res_ors ? 2 : 3];
|
||||||
|
BOOST_CHECK(sizeof(array_ors) / sizeof(char) == 2);
|
||||||
|
|
||||||
|
constexpr bool res_ands = !indeterminate(!(true && tribool(true) && true && indeterminate)); // false
|
||||||
|
BOOST_CHECK(!res_ands);
|
||||||
|
char array_ands[res_ands ? 2 : 3];
|
||||||
|
BOOST_CHECK(sizeof(array_ands) / sizeof(char) == 3);
|
||||||
|
|
||||||
|
// We avoid checking the tribool::operator safe_bool(),
|
||||||
|
// because GCC-4.8 fails to evaluate it at compile-time.
|
||||||
|
// Clang compiles well.
|
||||||
|
//
|
||||||
|
// constexpr bool res_safe_bool = tribool(true); // false
|
||||||
|
#endif
|
||||||
|
|
||||||
std::cout << "no errors detected\n";
|
std::cout << "no errors detected\n";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user