SFML对Vector2的实现允许以下语法:
sf::Vector2f v1(16.5f, 24.f);
sf::Vector2f v2 = v1 * 5.f;
sf::Vector2f v3;
...表示您在创建对象时不必指定template参数。我正试图出于自己的目的复制此代码,但无法查看其工作方式。看起来是这样的:
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;
};
我的尝试看起来像:
#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)
{
}
};
但是我仍然收到错误消息“缺少模板参数”。我不是做什么的SFML?
在您链接的源文件中,请注意底部附近的这一行:
typedef Vector2<float> Vector2f;
这将创建Vector2<float>
类型的别名Vector2f
-它们是相同的类型,只是可用于不同的名称。
您可以做同样的事情:
typedef Point<float> Pointf;
typedef Point<int> Pointi;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句