Reputation: 23
SFML's implementation of Vector2 allows the following syntax:
sf::Vector2f v1(16.5f, 24.f);
sf::Vector2f v2 = v1 * 5.f;
sf::Vector2f v3;
...meaning that you don't have to specify the template parameter when creating an object. I'm trying to reproduce this code for my own purposes but am unable to see how it works. Here's what it looks like:
template <typename T>
class Vector2
{
public :
Vector2();
Vector2(T X, T Y);
template <typename U>
explicit Vector2(const Vector2<U>& vector);
// Member data
T x;
T y;
};
And what my attempt looks like:
#include <type_traits>
template <typename T, typename = typename std::enable_if<
std::is_same<T, int>::value
|| std::is_same<T, float>::value, T>::type>
struct Point
{
T x, y;
Point(T x, T y) : x(x), y(y)
{
}
template <typename U>
explicit Point(const Point<U>& point)
: x(point.x), y(point.y)
{
}
};
But I still get an error 'missing template parameters'. What is SFML doing that I'm not?
Upvotes: 2
Views: 75
Reputation: 168988
In the source file you link, note this line near the bottom:
typedef Vector2<float> Vector2f;
This is creating an alias of the Vector2<float>
type as Vector2f
-- they are the same type, just usable by different names.
You could do the same thing:
typedef Point<float> Pointf;
typedef Point<int> Pointi;
Upvotes: 3
Reputation: 227390
Usually this is done via a typedef
:
typedef Vector2<float> Vector2f;
or a type alias:
using Vector2f = Vector2<float>;
The latter is not valid before C++11.
Upvotes: 3