From 3f4bb1ae12bb4694c27fb9d5af60e6f27357c0ee Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Thu, 5 Jan 2023 13:10:11 -0700 Subject: [PATCH] Prevent constructing `sf::Text` with a temporary `sf::Font` By deleting this constructor overload, it fails to compile if you pass a temporary font to this parameter slot. That includes code like sf::Text text("", sf::Font()); but more importantly it prohibits code like this sf::Font getFont() { sf::Font font; // load a font... return font; } sf::Text text("", getFont()); The same idea can be applied to setFont() to prevent setting fonts from a temporary. Credit to Jonny for the idea Co-authored-by: JonnyPtn --- include/SFML/Graphics/Text.hpp | 12 ++++++++++++ test/Graphics/Text.test.cpp | 1 + 2 files changed, 13 insertions(+) diff --git a/include/SFML/Graphics/Text.hpp b/include/SFML/Graphics/Text.hpp index c1b4dba5..f9153090 100644 --- a/include/SFML/Graphics/Text.hpp +++ b/include/SFML/Graphics/Text.hpp @@ -88,6 +88,12 @@ public: //////////////////////////////////////////////////////////// Text(const String& string, const Font& font, unsigned int characterSize = 30); + //////////////////////////////////////////////////////////// + /// \brief Disallow construction from a temporary font + /// + //////////////////////////////////////////////////////////// + Text(const String& string, Font&& font, unsigned int characterSize = 30) = delete; + //////////////////////////////////////////////////////////// /// \brief Copy constructor /// @@ -150,6 +156,12 @@ public: //////////////////////////////////////////////////////////// void setFont(const Font& font); + //////////////////////////////////////////////////////////// + /// \brief Disallow setting from a temporary font + /// + //////////////////////////////////////////////////////////// + void setFont(Font&& font) = delete; + //////////////////////////////////////////////////////////// /// \brief Set the character size /// diff --git a/test/Graphics/Text.test.cpp b/test/Graphics/Text.test.cpp index 9cf10197..bdc2ccdd 100644 --- a/test/Graphics/Text.test.cpp +++ b/test/Graphics/Text.test.cpp @@ -2,6 +2,7 @@ #include +static_assert(!std::is_constructible_v); static_assert(std::is_copy_constructible_v); static_assert(std::is_copy_assignable_v); static_assert(std::is_nothrow_move_constructible_v);