Skip to content

Commit

Permalink
Merge pull request #742 from lsst/tickets/DM-45834
Browse files Browse the repository at this point in the history
DM-45834: Fix C++17 deprecations and C++20 compilation
  • Loading branch information
mwittgen committed Aug 23, 2024
2 parents 7f10267 + 750a429 commit 569e0e2
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 19 deletions.
11 changes: 8 additions & 3 deletions include/lsst/afw/image/Pixel.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,14 @@ class Pixel : public detail::MaskedImagePixel_tag {
// Logical operators. We don't need to construct BinaryExpr for them
// as efficiency isn't a concern.
//
/// Return true iff two pixels are equal (in all three of image, mask, and variance)
template <typename T1>
friend bool operator==(Pixel const& lhs, T1 const& rhs) {
/// Return true if two pixels are equal (in all three of image, mask, and variance)
/// Fix C++20 compilation ambiguous overload problem by creating overloads
template <typename T1,typename T2, typename T3>
friend bool operator==(Pixel const& lhs, Pixel<T1,T2,T3> const& rhs) {
return lhs.image() == rhs.image() && lhs.mask() == rhs.mask() && lhs.variance() == rhs.variance();
}
template <typename T1,typename T2, typename T3>
friend bool operator==(Pixel const& lhs, SinglePixel<T1, T2, T3> const& rhs) {
return lhs.image() == rhs.image() && lhs.mask() == rhs.mask() && lhs.variance() == rhs.variance();
}

Expand Down
2 changes: 1 addition & 1 deletion include/lsst/afw/typehandling/GenericMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ class GenericMap {
// Assume that each operator() has the same return type; variant will enforce it
/// @cond
template <class Visitor>
using _VisitorResult = std::result_of_t<Visitor && (K&&, bool&)>;
using _VisitorResult = std::invoke_result_t<Visitor, K&&, bool&>;
/// @endcond

// No return value, const GenericMap
Expand Down
2 changes: 1 addition & 1 deletion include/lsst/afw/typehandling/Storable.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ LSST_EXCEPTION_TYPE(UnsupportedOperationException, pex::exceptions::RuntimeError
*/
class Storable : public table::io::Persistable {
public:
virtual ~Storable() noexcept = 0;
~Storable() noexcept override = 0;

/**
* Create a new object that is a copy of this one (optional operation).
Expand Down
23 changes: 14 additions & 9 deletions include/lsst/afw/typehandling/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ namespace test {

namespace {
class SimpleStorable : public Storable {
friend class ComplexStorable;
public:
virtual ~SimpleStorable() = default;
~SimpleStorable() override = default;

std::shared_ptr<Storable> cloneStorable() const override { return std::make_unique<SimpleStorable>(); }

Expand Down Expand Up @@ -87,17 +88,21 @@ class ComplexStorable final : public SimpleStorable {

std::size_t hash_value() const noexcept override { return std::hash<double>()(storage); }

// Warning: violates both substitution and equality symmetry!
// Fix violation of both substitution and equality symmetry
// as the code was broken on gcc with C++20 but passed on clang
bool equals(Storable const& other) const noexcept override {
auto complexOther = dynamic_cast<ComplexStorable const*>(&other);
if (complexOther) {
if (auto complexOther = dynamic_cast<ComplexStorable const*>(&other)) {
return this->storage == complexOther->storage;
} else {
return false;
}
return SimpleStorable::equals(other);
}
bool operator==(SimpleStorable const& other) const override { return this->equals(other); }

bool operator==(SimpleStorable const& other) const override {
if (auto complexOther = dynamic_cast<ComplexStorable const*>(&other)) {
return this->storage == complexOther->storage;
}
return SimpleStorable::operator==(other);
}
private:
double storage;
};
Expand All @@ -107,7 +112,7 @@ std::string universalToString(T const& value) {
std::stringstream buffer;
buffer << value;
return buffer.str();
};
}

// Would make more sense as static constants in GenericFactory
// but neither string nor Storable qualify as literal types
Expand Down Expand Up @@ -242,7 +247,7 @@ BOOST_TEST_CASE_TEMPLATE_FUNCTION(TestConstVisitor, GenericMapFactory) {
return universalToString(map->at(KEY6));
default:
throw std::invalid_argument("Bad key found");
};
}
};
std::vector<std::string> expected;
for (int key : mapKeys) {
Expand Down
10 changes: 5 additions & 5 deletions tests/test_polymorphicValue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ BOOST_AUTO_TEST_CASE(Copy) {
BOOST_CHECK(copy == original);

// Independent copy?
static_cast<ComplexStorable&>(copy.get()) = 4.2;
dynamic_cast<ComplexStorable&>(copy.get()) = 4.2;
BOOST_CHECK(copy != original);
}

Expand All @@ -126,7 +126,7 @@ BOOST_AUTO_TEST_CASE(Move) {
// changes to copy shouldn't affect original
auto postMove = PolymorphicValue(original);
BOOST_REQUIRE(original == postMove);
static_cast<ComplexStorable&>(copy.get()) = 4.2;
dynamic_cast<ComplexStorable&>(copy.get()) = 4.2;
BOOST_CHECK(original == postMove);
}

Expand All @@ -137,7 +137,7 @@ BOOST_AUTO_TEST_CASE(CopyAssign) {
BOOST_CHECK(copy == original);

// Independent copy?
static_cast<ComplexStorable&>(copy.get()) = 4.2;
dynamic_cast<ComplexStorable&>(copy.get()) = 4.2;
BOOST_CHECK(copy != original);
}

Expand All @@ -152,7 +152,7 @@ BOOST_AUTO_TEST_CASE(MoveAssign) {
// changes to copy shouldn't affect original
auto postMove = PolymorphicValue(original);
BOOST_REQUIRE(original == postMove);
static_cast<ComplexStorable&>(copy.get()) = 4.2;
dynamic_cast<ComplexStorable&>(copy.get()) = 4.2;
BOOST_CHECK(original == postMove);
}

Expand All @@ -168,7 +168,7 @@ BOOST_AUTO_TEST_CASE(ImplicitConversion) {
// Does modifying contents change holder?
Storable& contents = holder;
BOOST_REQUIRE(holder == PolymorphicValue(ComplexStorable(3.5)));
static_cast<ComplexStorable&>(contents) = 1.6;
dynamic_cast<ComplexStorable&>(contents) = 1.6;
BOOST_CHECK(holder != PolymorphicValue(ComplexStorable(3.5)));
}

Expand Down

0 comments on commit 569e0e2

Please sign in to comment.