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 <jonathan.r.paton@googlemail.com>
This commit is contained in:
Chris Thrasher 2023-01-05 13:10:11 -07:00
parent 718195bf25
commit 3f4bb1ae12
2 changed files with 13 additions and 0 deletions

View File

@ -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
///

View File

@ -2,6 +2,7 @@
#include <type_traits>
static_assert(!std::is_constructible_v<sf::Text, sf::String, sf::Font&&, unsigned int>);
static_assert(std::is_copy_constructible_v<sf::Text>);
static_assert(std::is_copy_assignable_v<sf::Text>);
static_assert(std::is_nothrow_move_constructible_v<sf::Text>);