From de9286c11f8b11b262f07dc47e87fdffdd105d21 Mon Sep 17 00:00:00 2001 From: ZXShady <153229951+ZXShady@users.noreply.github.com> Date: Sun, 29 Sep 2024 23:18:48 +0100 Subject: [PATCH] Use explicit conversion operator instead of explicit constructor I also added a type trait test to check whether the conversion operator is marked explicit. this change has the advantage of being clearer imo as it is a conversion operator for conersions not a constructor. and also it allows users to take the address of convesion operator which I don't know if someone might find that useful. --- include/SFML/Graphics/Glsl.inl | 11 +++-------- include/SFML/Graphics/Rect.hpp | 11 ++--------- include/SFML/Graphics/Rect.inl | 3 ++- include/SFML/System/Vector2.hpp | 11 ++--------- include/SFML/System/Vector2.inl | 3 ++- include/SFML/System/Vector3.hpp | 11 ++--------- include/SFML/System/Vector3.inl | 6 ++---- test/Graphics/Glsl.test.cpp | 12 +++++++++--- test/Graphics/Rect.test.cpp | 20 +++++++++++--------- test/System/Vector2.test.cpp | 4 +++- test/System/Vector3.test.cpp | 4 +++- 11 files changed, 41 insertions(+), 55 deletions(-) diff --git a/include/SFML/Graphics/Glsl.inl b/include/SFML/Graphics/Glsl.inl index a4a66e20c..f1f91f3ee 100644 --- a/include/SFML/Graphics/Glsl.inl +++ b/include/SFML/Graphics/Glsl.inl @@ -128,18 +128,13 @@ struct Vector4 #endif //////////////////////////////////////////////////////////// - /// \brief Conversion constructor - /// - /// \param other 4D vector of different type + /// \brief Converts the vector to another type of vector /// //////////////////////////////////////////////////////////// template - constexpr explicit Vector4(const Vector4& other) : - x(static_cast(other.x)), - y(static_cast(other.y)), - z(static_cast(other.z)), - w(static_cast(other.w)) + constexpr explicit operator Vector4() const { + return Vector4(static_cast(x), static_cast(y), static_cast(z), static_cast(w)); } //////////////////////////////////////////////////////////// diff --git a/include/SFML/Graphics/Rect.hpp b/include/SFML/Graphics/Rect.hpp index 0e8650804..d70332889 100644 --- a/include/SFML/Graphics/Rect.hpp +++ b/include/SFML/Graphics/Rect.hpp @@ -64,18 +64,11 @@ public: constexpr Rect(Vector2 position, Vector2 size); //////////////////////////////////////////////////////////// - /// \brief Construct the rectangle from another type of rectangle - /// - /// This constructor doesn't replace the copy constructor, - /// it's called only when U != T. - /// A call to this constructor will fail to compile if U - /// is not convertible to T. - /// - /// \param rectangle Rectangle to convert + /// \brief Converts the rectangle to another type of rectangle /// //////////////////////////////////////////////////////////// template - constexpr explicit Rect(const Rect& rectangle); + constexpr explicit operator Rect() const; //////////////////////////////////////////////////////////// /// \brief Check if a point is inside the rectangle's area diff --git a/include/SFML/Graphics/Rect.inl b/include/SFML/Graphics/Rect.inl index 4af65f120..e1dc35fec 100644 --- a/include/SFML/Graphics/Rect.inl +++ b/include/SFML/Graphics/Rect.inl @@ -40,8 +40,9 @@ constexpr Rect::Rect(Vector2 thePosition, Vector2 theSize) : position(t //////////////////////////////////////////////////////////// template template -constexpr Rect::Rect(const Rect& rectangle) : position(rectangle.position), size(rectangle.size) +constexpr Rect::operator Rect() const { + return Rect(Vector2(position), Vector2(size)); } diff --git a/include/SFML/System/Vector2.hpp b/include/SFML/System/Vector2.hpp index 93a5ec7d4..d36eb07ea 100644 --- a/include/SFML/System/Vector2.hpp +++ b/include/SFML/System/Vector2.hpp @@ -58,18 +58,11 @@ public: constexpr Vector2(T x, T y); //////////////////////////////////////////////////////////// - /// \brief Construct the vector from another type of vector - /// - /// This constructor doesn't replace the copy constructor, - /// it's called only when U != T. - /// A call to this constructor will fail to compile if U - /// is not convertible to T. - /// - /// \param vector Vector to convert + /// \brief Converts the vector to another type of vector /// //////////////////////////////////////////////////////////// template - constexpr explicit Vector2(Vector2 vector); + constexpr explicit operator Vector2() const; //////////////////////////////////////////////////////////// /// \brief Construct the vector from polar coordinates (floating-point) diff --git a/include/SFML/System/Vector2.inl b/include/SFML/System/Vector2.inl index 8955dd382..a330c1e4d 100644 --- a/include/SFML/System/Vector2.inl +++ b/include/SFML/System/Vector2.inl @@ -49,8 +49,9 @@ constexpr Vector2::Vector2(T x, T y) : x(x), y(y) //////////////////////////////////////////////////////////// template template -constexpr Vector2::Vector2(Vector2 vector) : x(static_cast(vector.x)), y(static_cast(vector.y)) +constexpr Vector2::operator Vector2() const { + return Vector2(static_cast(x), static_cast(y)); } diff --git a/include/SFML/System/Vector3.hpp b/include/SFML/System/Vector3.hpp index 49e6674a2..ec54d58ad 100644 --- a/include/SFML/System/Vector3.hpp +++ b/include/SFML/System/Vector3.hpp @@ -57,18 +57,11 @@ public: constexpr Vector3(T x, T y, T z); //////////////////////////////////////////////////////////// - /// \brief Construct the vector from another type of vector - /// - /// This constructor doesn't replace the copy constructor, - /// it's called only when U != T. - /// A call to this constructor will fail to compile if U - /// is not convertible to T. - /// - /// \param vector Vector to convert + /// \brief Converts the vector to another type of vector /// //////////////////////////////////////////////////////////// template - constexpr explicit Vector3(const Vector3& vector); + constexpr explicit operator Vector3() const; //////////////////////////////////////////////////////////// /// \brief Length of the vector (floating-point). diff --git a/include/SFML/System/Vector3.inl b/include/SFML/System/Vector3.inl index 4baa5a48c..fa9f44485 100644 --- a/include/SFML/System/Vector3.inl +++ b/include/SFML/System/Vector3.inl @@ -49,11 +49,9 @@ constexpr Vector3::Vector3(T x, T y, T z) : x(x), y(y), z(z) //////////////////////////////////////////////////////////// template template -constexpr Vector3::Vector3(const Vector3& vector) : -x(static_cast(vector.x)), -y(static_cast(vector.y)), -z(static_cast(vector.z)) +constexpr Vector3::operator Vector3() const { + return Vector3(static_cast(x), static_cast(y), static_cast(z)); } diff --git a/test/Graphics/Glsl.test.cpp b/test/Graphics/Glsl.test.cpp index b9d68a9a5..68c685ac8 100644 --- a/test/Graphics/Glsl.test.cpp +++ b/test/Graphics/Glsl.test.cpp @@ -133,8 +133,10 @@ TEST_CASE("[Graphics] sf::Glsl") STATIC_CHECK(vec.w == 4); } - SECTION("Conversion constructor") + SECTION("Conversion operator") { + STATIC_CHECK(!std::is_convertible_v); + constexpr sf::Glsl::Ivec4 ivec(10, 12, 14, 16); constexpr sf::Glsl::Vec4 vec(ivec); STATIC_CHECK(vec.x == 10); @@ -181,8 +183,10 @@ TEST_CASE("[Graphics] sf::Glsl") STATIC_CHECK(vec.w == 4); } - SECTION("Conversion constructor") + SECTION("Conversion operator") { + STATIC_CHECK(!std::is_convertible_v); + constexpr sf::Glsl::Bvec4 bvec(true, false, true, false); constexpr sf::Glsl::Ivec4 vec(bvec); STATIC_CHECK(vec.x == 1); @@ -229,8 +233,10 @@ TEST_CASE("[Graphics] sf::Glsl") STATIC_CHECK(vec.w == false); } - SECTION("Conversion constructor") + SECTION("Conversion operator") { + STATIC_CHECK(!std::is_convertible_v); + constexpr sf::Glsl::Ivec4 ivec(0, -7, 2, 10); constexpr sf::Glsl::Bvec4 vec(ivec); STATIC_CHECK(vec.x == false); diff --git a/test/Graphics/Rect.test.cpp b/test/Graphics/Rect.test.cpp index 384d26989..edb94a710 100644 --- a/test/Graphics/Rect.test.cpp +++ b/test/Graphics/Rect.test.cpp @@ -35,15 +35,6 @@ TEMPLATE_TEST_CASE("[Graphics] sf::Rect", "", int, float) STATIC_CHECK(rectangle.position == position); STATIC_CHECK(rectangle.size == dimension); } - - SECTION("Conversion constructor") - { - constexpr sf::FloatRect sourceRectangle({1.0f, 2.0f}, {3.0f, 4.0f}); - constexpr sf::IntRect rectangle(sourceRectangle); - - STATIC_CHECK(rectangle.position == sf::Vector2i(1, 2)); - STATIC_CHECK(rectangle.size == sf::Vector2i(3, 4)); - } } SECTION("contains(Vector2)") @@ -81,6 +72,17 @@ TEMPLATE_TEST_CASE("[Graphics] sf::Rect", "", int, float) SECTION("Operators") { + SECTION("operator Rect") + { + STATIC_CHECK(!std::is_convertible_v); + + constexpr sf::FloatRect sourceRectangle({1.0f, 2.0f}, {3.0f, 4.0f}); + constexpr sf::IntRect rectangle(sourceRectangle); + + STATIC_CHECK(rectangle.position == sf::Vector2i(1, 2)); + STATIC_CHECK(rectangle.size == sf::Vector2i(3, 4)); + } + SECTION("operator==") { STATIC_CHECK(sf::Rect() == sf::Rect()); diff --git a/test/System/Vector2.test.cpp b/test/System/Vector2.test.cpp index cb0622fc6..933adbd2c 100644 --- a/test/System/Vector2.test.cpp +++ b/test/System/Vector2.test.cpp @@ -36,8 +36,10 @@ TEMPLATE_TEST_CASE("[System] sf::Vector2", "", int, float) STATIC_CHECK(vector.y == 2); } - SECTION("Conversion constructor") + SECTION("Conversion operator") { + STATIC_CHECK(!std::is_convertible_v); + constexpr sf::Vector2f sourceVector(1.0f, 2.0f); constexpr sf::Vector2i vector(sourceVector); diff --git a/test/System/Vector3.test.cpp b/test/System/Vector3.test.cpp index 29da14bba..d641d3db6 100644 --- a/test/System/Vector3.test.cpp +++ b/test/System/Vector3.test.cpp @@ -33,8 +33,10 @@ TEMPLATE_TEST_CASE("[System] sf::Vector3", "", int, float) STATIC_CHECK(vector.z == 3); } - SECTION("Conversion constructor") + SECTION("Conversion operator") { + STATIC_CHECK(!std::is_convertible_v); + constexpr sf::Vector3f sourceVector(1.0f, 2.0f, 3.0f); constexpr sf::Vector3i vector(sourceVector);