SFML community forums

General => Feature requests => Topic started by: Disch on April 19, 2010, 12:09:54 am

Title: Ctor to handle implicit casting between Vector2<T> typ
Post by: Disch 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.
Title: Ctor to handle implicit casting between Vector2<T> typ
Post by: Laurent 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 ;)
Title: Ctor to handle implicit casting between Vector2<T> typ
Post by: Nexus 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)
Title: Ctor to handle implicit casting between Vector2<T> typ
Post by: Disch on April 20, 2010, 06:28:19 am
Point taken.

How about an explicit ctor then?