Add explicit to bool operators of Point and Rect (#4948)

Found a bug where the following won't work:
```c++
COORD inclusiveEnd{ _end };
```
where `_end` is a `til::point`.

The only fix for this is to replace these instances with this:
```c++
COORD inclusiveEnd = _end;
```

What was happening in the first notation is the implicit conversion of `til::point` to `bool` to `SHORT`. The constructor for COORD only sees one SHORT so it thinks the value should be the definition for X, and Y should stay as 0. So we end up getting `1, 0`.

By adding the explicit keyword to the bool operators, we prevent the accident above from occurring.
This commit is contained in:
Carlos Zamora 2020-03-19 09:12:15 -07:00 committed by GitHub
parent 9e9473cfb2
commit ae3f8f3759
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 21 deletions

View file

@ -64,11 +64,6 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
return !(*this == other);
}
operator bool() const noexcept
{
return _x != 0 || _y != 0;
}
constexpr bool operator<(const point& other) const noexcept
{
if (_y < other._y)

View file

@ -190,7 +190,7 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
return !(*this == other);
}
constexpr operator bool() const noexcept
explicit constexpr operator bool() const noexcept
{
return _topLeft.x() < _bottomRight.x() &&
_topLeft.y() < _bottomRight.y();

View file

@ -64,6 +64,11 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
return !(*this == other);
}
constexpr explicit operator bool() const noexcept
{
return _width > 0 && _height > 0;
}
size operator+(const size& other) const
{
ptrdiff_t width;

View file

@ -168,21 +168,6 @@ class PointTests
}
}
TEST_METHOD(Boolean)
{
const til::point empty;
VERIFY_IS_FALSE(empty);
const til::point yOnly{ 0, 10 };
VERIFY_IS_TRUE(yOnly);
const til::point xOnly{ 10, 0 };
VERIFY_IS_TRUE(xOnly);
const til::point both{ 10, 10 };
VERIFY_IS_TRUE(both);
}
TEST_METHOD(Addition)
{
Log::Comment(L"0.) Addition of two things that should be in bounds.");

View file

@ -168,6 +168,30 @@ class SizeTests
}
}
TEST_METHOD(Boolean)
{
const til::size empty;
VERIFY_IS_FALSE(!!empty);
const til::size yOnly{ 0, 10 };
VERIFY_IS_FALSE(!!yOnly);
const til::size xOnly{ 10, 0 };
VERIFY_IS_FALSE(!!xOnly);
const til::size both{ 10, 10 };
VERIFY_IS_TRUE(!!both);
const til::size yNegative{ 10, -10 };
VERIFY_IS_FALSE(!!yNegative);
const til::size xNegative{ -10, 10 };
VERIFY_IS_FALSE(!!xNegative);
const til::size bothNegative{ -10, -10 };
VERIFY_IS_FALSE(!!bothNegative);
}
TEST_METHOD(Addition)
{
Log::Comment(L"0.) Addition of two things that should be in bounds.");