mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-29 18:07:16 +02:00
feat: constructors of cartesian_vector
refactored
This commit is contained in:
@ -53,26 +53,30 @@ public:
|
|||||||
T _coordinates_[3];
|
T _coordinates_[3];
|
||||||
using value_type = T;
|
using value_type = T;
|
||||||
|
|
||||||
cartesian_vector() = default;
|
|
||||||
cartesian_vector(const cartesian_vector&) = default;
|
cartesian_vector(const cartesian_vector&) = default;
|
||||||
cartesian_vector(cartesian_vector&&) = default;
|
cartesian_vector(cartesian_vector&&) = default;
|
||||||
cartesian_vector& operator=(const cartesian_vector&) = default;
|
cartesian_vector& operator=(const cartesian_vector&) = default;
|
||||||
cartesian_vector& operator=(cartesian_vector&&) = default;
|
cartesian_vector& operator=(cartesian_vector&&) = default;
|
||||||
|
|
||||||
template<std::convertible_to<T> Arg1, std::convertible_to<T>... Args>
|
template<typename... Args>
|
||||||
constexpr cartesian_vector(Arg1&& arg1, Args&&... args) :
|
requires(... && std::constructible_from<T, Args>)
|
||||||
_coordinates_(std::forward<Arg1>(arg1), std::forward<Args>(args)...)
|
constexpr explicit(!(... && std::convertible_to<Args, T>)) cartesian_vector(Args&&... args) :
|
||||||
|
_coordinates_{static_cast<T>(std::forward<Args>(args))...}
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
template<std::convertible_to<T> U>
|
template<typename U>
|
||||||
constexpr cartesian_vector(const cartesian_vector<U>& other) : _coordinates_{other[0], other[1], other[2]}
|
requires std::constructible_from<T, U>
|
||||||
|
constexpr explicit(!std::convertible_to<U, T>) cartesian_vector(const cartesian_vector<U>& other) :
|
||||||
|
_coordinates_{static_cast<T>(other[0]), static_cast<T>(other[1]), static_cast<T>(other[2])}
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
template<std::convertible_to<T> U>
|
template<typename U>
|
||||||
constexpr cartesian_vector(cartesian_vector<U>&& other) :
|
requires std::constructible_from<T, U>
|
||||||
_coordinates_{std::move(other[0]), std::move(other[1]), std::move(other[2])}
|
constexpr explicit(!std::convertible_to<U, T>) cartesian_vector(cartesian_vector<U>&& other) :
|
||||||
|
_coordinates_{static_cast<T>(std::move(other[0])), static_cast<T>(std::move(other[1])),
|
||||||
|
static_cast<T>(std::move(other[2]))}
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +43,22 @@ TEST_CASE("cartesian_vector operations", "[vector]")
|
|||||||
{
|
{
|
||||||
SECTION("cartesian_vector initialization and access")
|
SECTION("cartesian_vector initialization and access")
|
||||||
{
|
{
|
||||||
|
SECTION("no arguments")
|
||||||
|
{
|
||||||
|
cartesian_vector<double> v;
|
||||||
|
REQUIRE(v[0] == 0);
|
||||||
|
REQUIRE(v[1] == 0);
|
||||||
|
REQUIRE(v[2] == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("zero arguments")
|
||||||
|
{
|
||||||
|
cartesian_vector<double> v{};
|
||||||
|
REQUIRE(v[0] == 0);
|
||||||
|
REQUIRE(v[1] == 0);
|
||||||
|
REQUIRE(v[2] == 0);
|
||||||
|
}
|
||||||
|
|
||||||
SECTION("one argument")
|
SECTION("one argument")
|
||||||
{
|
{
|
||||||
cartesian_vector v{1.0};
|
cartesian_vector v{1.0};
|
||||||
@ -66,6 +82,14 @@ TEST_CASE("cartesian_vector operations", "[vector]")
|
|||||||
REQUIRE(v[1] == 2.0);
|
REQUIRE(v[1] == 2.0);
|
||||||
REQUIRE(v[2] == 3.0);
|
REQUIRE(v[2] == 3.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("convertible arguments")
|
||||||
|
{
|
||||||
|
cartesian_vector<double> v{1, 2, 3};
|
||||||
|
REQUIRE(v[0] == 1.0);
|
||||||
|
REQUIRE(v[1] == 2.0);
|
||||||
|
REQUIRE(v[2] == 3.0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("convertibility")
|
SECTION("convertibility")
|
||||||
|
Reference in New Issue
Block a user