Skip to content

Commit

Permalink
Revert "refactor: make_reference used to return values from referen…
Browse files Browse the repository at this point in the history
…ce operations"

This reverts commit 963256a.
  • Loading branch information
mpusz committed Feb 21, 2024
1 parent 963256a commit e6b9a5c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 62 deletions.
1 change: 0 additions & 1 deletion src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ add_mp_units_module(
include/mp-units/bits/get_associated_quantity.h
include/mp-units/bits/get_common_base.h
include/mp-units/bits/magnitude.h
include/mp-units/bits/make_reference.h
include/mp-units/bits/quantity_cast.h
include/mp-units/bits/quantity_concepts.h
include/mp-units/bits/quantity_point_concepts.h
Expand Down
39 changes: 0 additions & 39 deletions src/core/include/mp-units/bits/make_reference.h

This file was deleted.

12 changes: 11 additions & 1 deletion src/core/include/mp-units/quantity_spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <mp-units/bits/external/type_name.h>
#include <mp-units/bits/external/type_traits.h>
#include <mp-units/bits/get_common_base.h>
#include <mp-units/bits/make_reference.h>
#include <mp-units/bits/quantity_concepts.h>
#include <mp-units/bits/quantity_spec_concepts.h>
#include <mp-units/bits/reference_concepts.h>
Expand All @@ -40,6 +39,17 @@ namespace mp_units {

namespace detail {


template<QuantitySpec QS, Unit U>
requires(!AssociatedUnit<U>) || UnitOf<U, QS{}>
[[nodiscard]] consteval Reference auto make_reference(QS, U u)
{
if constexpr (detail::QuantityKindSpec<QS>)
return u;
else
return reference<QS, U>{};
}

// TODO revise the note in the below comment
/**
* @brief Returns the most restrictive character from the list
Expand Down
84 changes: 63 additions & 21 deletions src/core/include/mp-units/reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

#pragma once

#include <mp-units/bits/external/hacks.h>
#include <mp-units/bits/get_associated_quantity.h>
#include <mp-units/bits/make_reference.h>
#include <mp-units/bits/quantity_concepts.h>
#include <mp-units/bits/reference_concepts.h>
#include <mp-units/bits/representation_concepts.h>
Expand Down Expand Up @@ -68,44 +68,74 @@ struct reference {
}

template<typename Q2, typename U2>
[[nodiscard]] friend consteval Reference auto operator*(reference, reference<Q2, U2>)
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(Q{} * Q2{})>,
std::remove_const_t<decltype(U{} * U2{})>>
operator*(reference, reference<Q2, U2>)
{
return detail::make_reference(Q{} * Q2{}, U{} * U2{});
return {};
}

template<AssociatedUnit U2>
[[nodiscard]] friend consteval Reference auto operator*(reference, U2)
#if MP_UNITS_COMP_MSVC
[[nodiscard]] friend consteval decltype(reference<Q * get_quantity_spec(U2{}), U * U2{}>{}) operator*(reference, U2)
#else
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(Q{} * get_quantity_spec(U2{}))>,
std::remove_const_t<decltype(U{} * U2{})>>
operator*(reference, U2)
#endif
{
return detail::make_reference(Q{} * get_quantity_spec(U2{}), U{} * U2{});
return {};
}

template<AssociatedUnit U1>
[[nodiscard]] friend consteval Reference auto operator*(U1, reference)
#if MP_UNITS_COMP_MSVC
[[nodiscard]] friend consteval decltype(reference<get_quantity_spec(U1{}) * Q, U1{} * U>{}) operator*(U1, reference)
#else
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(get_quantity_spec(U1{}) * Q{})>,
std::remove_const_t<decltype(U1{} * U{})>>
operator*(U1, reference)
#endif
{
return detail::make_reference(get_quantity_spec(U1{}) * Q{}, U1{} * U{});
return {};
}

template<typename Q2, typename U2>
[[nodiscard]] friend consteval Reference auto operator/(reference, reference<Q2, U2>)
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(Q{} / Q2{})>,
std::remove_const_t<decltype(U{} / U2{})>>
operator/(reference, reference<Q2, U2>)
{
return detail::make_reference(Q{} / Q2{}, U{} / U2{});
return {};
}

template<AssociatedUnit U2>
[[nodiscard]] friend consteval Reference auto operator/(reference, U2)
#if MP_UNITS_COMP_MSVC
[[nodiscard]] friend consteval decltype(reference<Q / get_quantity_spec(U2{}), U / U2{}>{}) operator/(reference, U2)
#else
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(Q{} / get_quantity_spec(U2{}))>,
std::remove_const_t<decltype(U{} / U2{})>>
operator/(reference, U2)
#endif
{
return detail::make_reference(Q{} / get_quantity_spec(U2{}), U{} / U2{});
return {};
}

template<AssociatedUnit U1>
[[nodiscard]] friend consteval Reference auto operator/(U1, reference)
#if MP_UNITS_COMP_MSVC
[[nodiscard]] friend consteval decltype(reference<get_quantity_spec(U1{}) / Q, U1{} / U>{}) operator/(U1, reference)
#else
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(get_quantity_spec(U1{}) / Q{})>,
std::remove_const_t<decltype(U1{} / U{})>>
operator/(U1, reference)
#endif
{
return detail::make_reference(get_quantity_spec(U1{}) / Q{}, U1{} / U{});
return {};
}

[[nodiscard]] friend consteval Reference auto inverse(reference)
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(inverse(Q{}))>,
std::remove_const_t<decltype(inverse(U{}))>>
inverse(reference)
{
return detail::make_reference(inverse(Q{}), inverse(U{}));
return {};
}

/**
Expand All @@ -119,9 +149,11 @@ struct reference {
*/
template<std::intmax_t Num, std::intmax_t Den = 1>
requires detail::non_zero<Den>
[[nodiscard]] friend consteval Reference auto pow(reference)
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(pow<Num, Den>(Q{}))>,
std::remove_const_t<decltype(pow<Num, Den>(U{}))>>
pow(reference)
{
return detail::make_reference(pow<Num, Den>(Q{}), pow<Num, Den>(U{}));
return {};
}

/**
Expand All @@ -131,7 +163,12 @@ struct reference {
*
* @return The result of computation
*/
[[nodiscard]] friend consteval Reference auto sqrt(reference) { return detail::make_reference(sqrt(Q{}), sqrt(U{})); }
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(sqrt(Q{}))>,
std::remove_const_t<decltype(sqrt(U{}))>>
sqrt(reference)
{
return {};
}

/**
* @brief Computes the cubic root of a reference
Expand All @@ -140,7 +177,12 @@ struct reference {
*
* @return The result of computation
*/
[[nodiscard]] friend consteval Reference auto cbrt(reference) { return detail::make_reference(cbrt(Q{}), cbrt(U{})); }
[[nodiscard]] friend consteval reference<std::remove_const_t<decltype(cbrt(Q{}))>,
std::remove_const_t<decltype(cbrt(U{}))>>
cbrt(reference)
{
return {};
}

template<typename Q2, typename U2>
[[nodiscard]] friend consteval bool convertible(reference, reference<Q2, U2>)
Expand Down Expand Up @@ -247,9 +289,9 @@ template<AssociatedUnit auto To, AssociatedUnit From>
}

template<Unit auto To, QuantitySpec QS, Unit U>
[[nodiscard]] consteval Reference auto clone_reference_with(reference<QS, U>)
[[nodiscard]] consteval reference<QS, std::remove_const_t<decltype(To)>> clone_reference_with(reference<QS, U>)
{
return detail::make_reference(QS{}, To);
return {};
}

} // namespace detail
Expand Down

0 comments on commit e6b9a5c

Please sign in to comment.