measurement example updated with a starship operator

This commit is contained in:
Mateusz Pusz
2019-12-17 09:12:11 +01:00
parent d5f7de8ecb
commit 1b2d27a64c

View File

@@ -40,8 +40,10 @@ namespace {
measurement() = default;
constexpr /* explicit */ measurement(const value_type& val, const value_type& err = {}): // cannot be explicit as `magma` concept requires implicit conversions :-(
value_(val), uncertainty_(std::abs(err))
constexpr /* explicit */ measurement(const value_type& val, const value_type& err = {}) :
// cannot be explicit as `magma` concept requires implicit conversions :-(
value_(val),
uncertainty_(std::abs(err))
{
}
@@ -76,6 +78,13 @@ namespace {
return measurement(val, val * rss(lhs.relative_uncertainty(), rhs.relative_uncertainty()));
}
#if __GNUC__ >= 10
[[nodiscard]] friend constexpr auto operator<=>(const measurement& lhs, const measurement& rhs) = default;
[[nodiscard]] friend constexpr bool operator==(const measurement& lhs, const measurement& rhs) = default;
#else
[[nodiscard]] friend constexpr bool operator==(const measurement& lhs, const measurement& rhs)
{
return lhs.value() == rhs.value() && lhs.uncertainty() == rhs.uncertainty();
@@ -91,10 +100,7 @@ namespace {
return lhs.value() == rhs.value() ? lhs.uncertainty() < rhs.uncertainty() : lhs.value() < rhs.value();
}
[[nodiscard]] friend constexpr bool operator>(const measurement& lhs, const measurement& rhs)
{
return rhs < lhs;
}
[[nodiscard]] friend constexpr bool operator>(const measurement& lhs, const measurement& rhs) { return rhs < lhs; }
[[nodiscard]] friend constexpr bool operator<=(const measurement& lhs, const measurement& rhs)
{
@@ -106,6 +112,8 @@ namespace {
return !(lhs < rhs);
}
#endif
friend std::ostream& operator<<(std::ostream& os, const measurement& v)
{
return os << v.value() << " ± " << v.uncertainty();
@@ -116,6 +124,8 @@ namespace {
value_type uncertainty_{};
};
static_assert(units::Scalar<measurement<double>>);
} // namespace
template<typename T>
@@ -143,11 +153,9 @@ int main()
{
try {
example();
}
catch (const std::exception& ex) {
} catch (const std::exception& ex) {
std::cerr << "Unhandled std exception caught: " << ex.what() << '\n';
}
catch (...) {
} catch (...) {
std::cerr << "Unhandled unknown exception caught\n";
}
}