Commit 12c01784 authored by Marcus Holland-Moritz's avatar Marcus Holland-Moritz Committed by Facebook Github Bot 6

Allow unchecked conversion from floating point to bool

Summary:
In order to be consistent with integral-to-bool conversion, this
change allows conversion from floating point values to bool following
the same rule that is to be consistent with C(++) conventions. Also,
any arithmetic value can be converted to bool without potential for
undefined behaviour, so no extra checks are required.

Differential Revision: D3483760

fbshipit-source-id: 024b58d348ef679079aba4d9d5277acb46aba2a1
parent 6dbd5d06
...@@ -83,24 +83,27 @@ to(Src && value) { ...@@ -83,24 +83,27 @@ to(Src && value) {
} }
/******************************************************************************* /*******************************************************************************
* Integral to integral * Arithmetic to boolean
******************************************************************************/ ******************************************************************************/
/** /**
* Unchecked conversion from integral to boolean. This is different from the * Unchecked conversion from arithmetic to boolean. This is different from the
* other integral conversions because we use the C convention of treating any * other arithmetic conversions because we use the C convention of treating any
* non-zero value as true, instead of range checking. * non-zero value as true, instead of range checking.
*/ */
template <class Tgt, class Src> template <class Tgt, class Src>
typename std::enable_if< typename std::enable_if<
std::is_integral<Src>::value std::is_arithmetic<Src>::value && !std::is_same<Tgt, Src>::value &&
&& !std::is_same<Tgt, Src>::value std::is_same<Tgt, bool>::value,
&& std::is_same<Tgt, bool>::value, Tgt>::type
Tgt>::type to(const Src& value) {
to(const Src & value) { return value != Src();
return value != 0;
} }
/*******************************************************************************
* Integral to integral
******************************************************************************/
/** /**
* Checked conversion from integral to integral. The checks are only * Checked conversion from integral to integral. The checks are only
* performed when meaningful, e.g. conversion from int to long goes * performed when meaningful, e.g. conversion from int to long goes
...@@ -1220,11 +1223,11 @@ checkConversion(const Src&) { ...@@ -1220,11 +1223,11 @@ checkConversion(const Src&) {
*/ */
template <class Tgt, class Src> template <class Tgt, class Src>
typename std::enable_if< typename std::enable_if<
(std::is_integral<Src>::value && std::is_floating_point<Tgt>::value) (std::is_integral<Src>::value && std::is_floating_point<Tgt>::value) ||
|| (std::is_floating_point<Src>::value && std::is_integral<Tgt>::value &&
(std::is_floating_point<Src>::value && std::is_integral<Tgt>::value), !std::is_same<Tgt, bool>::value),
Tgt>::type Tgt>::type
to(const Src & value) { to(const Src& value) {
if (detail::checkConversion<Tgt>(value)) { if (detail::checkConversion<Tgt>(value)) {
Tgt result = Tgt(value); Tgt result = Tgt(value);
if (detail::checkConversion<Src>(result)) { if (detail::checkConversion<Src>(result)) {
......
...@@ -840,6 +840,13 @@ TEST(Conv, BoolToFloat) { ...@@ -840,6 +840,13 @@ TEST(Conv, BoolToFloat) {
TEST(Conv, FloatToBool) { TEST(Conv, FloatToBool) {
EXPECT_EQ(to<bool>(1.0), true); EXPECT_EQ(to<bool>(1.0), true);
EXPECT_EQ(to<bool>(0.0), false); EXPECT_EQ(to<bool>(0.0), false);
EXPECT_EQ(to<bool>(2.7), true);
EXPECT_EQ(to<bool>(std::numeric_limits<double>::max()), true);
EXPECT_EQ(to<bool>(std::numeric_limits<double>::min()), true);
EXPECT_EQ(to<bool>(std::numeric_limits<double>::lowest()), true);
EXPECT_EQ(to<bool>(std::numeric_limits<double>::quiet_NaN()), true);
EXPECT_EQ(to<bool>(std::numeric_limits<double>::infinity()), true);
EXPECT_EQ(to<bool>(-std::numeric_limits<double>::infinity()), true);
} }
TEST(Conv, NewUint64ToString) { TEST(Conv, NewUint64ToString) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment