Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Ctor to handle implicit casting between Vector2<T> typ  (Read 2438 times)

0 Members and 1 Guest are viewing this topic.

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Ctor to handle implicit casting between Vector2<T> typ
« on: April 19, 2010, 12:09:54 am »
I use sf::Vector2<double> in calculations for extra precision, but pretty much everything in SFML takes sf::Vector2<float>.

It would be nice if sf::Vector2 had a ctor that could convert between different types so that these could be implicitly cast.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Ctor to handle implicit casting between Vector2<T> typ
« Reply #1 on: April 19, 2010, 08:09:16 am »
Such an implicit cast would be dangerous, hiding conversions that lose precision is always bad.

That's why your compiler issues a warning when you type "int x = 5.0", and you must use a static_cast ;)
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Ctor to handle implicit casting between Vector2<T> typ
« Reply #2 on: April 19, 2010, 03:01:25 pm »
Or write a vector cast function.
Code: [Select]
template <typename Dest, typename Source>
sf::Vector2<Dest> ConvertVector(const Vector<Source>& source)
{
    return sf::Vector2<Dest>(
        static_cast<Dest>(source.x),
        static_cast<Dest>(source.y));
}

sf::Vector2<double> doubleVec;
sf::Vector2<float> floatVec = ConvertVector<float>(doubleVec)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Ctor to handle implicit casting between Vector2<T> typ
« Reply #3 on: April 20, 2010, 06:28:19 am »
Point taken.

How about an explicit ctor then?