feat: constructors of cartesian_vector refactored

This commit is contained in:
Mateusz Pusz
2024-11-14 20:30:12 +01:00
parent 7e1baf65fa
commit 4b5d37e83e
2 changed files with 37 additions and 9 deletions

View File

@ -53,26 +53,30 @@ public:
T _coordinates_[3];
using value_type = T;
cartesian_vector() = default;
cartesian_vector(const cartesian_vector&) = default;
cartesian_vector(cartesian_vector&&) = default;
cartesian_vector& operator=(const cartesian_vector&) = default;
cartesian_vector& operator=(cartesian_vector&&) = default;
template<std::convertible_to<T> Arg1, std::convertible_to<T>... Args>
constexpr cartesian_vector(Arg1&& arg1, Args&&... args) :
_coordinates_(std::forward<Arg1>(arg1), std::forward<Args>(args)...)
template<typename... Args>
requires(... && std::constructible_from<T, 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>
constexpr cartesian_vector(const cartesian_vector<U>& other) : _coordinates_{other[0], other[1], other[2]}
template<typename U>
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>
constexpr cartesian_vector(cartesian_vector<U>&& other) :
_coordinates_{std::move(other[0]), std::move(other[1]), std::move(other[2])}
template<typename U>
requires std::constructible_from<T, U>
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]))}
{
}

View File

@ -43,6 +43,22 @@ TEST_CASE("cartesian_vector operations", "[vector]")
{
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")
{
cartesian_vector v{1.0};
@ -66,6 +82,14 @@ TEST_CASE("cartesian_vector operations", "[vector]")
REQUIRE(v[1] == 2.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")