Improved constexpr support

This commit is contained in:
Groove 2018-07-29 00:33:09 +02:00
parent 178314a67d
commit 472f2c13b7
11 changed files with 46 additions and 30 deletions

View File

@ -61,14 +61,14 @@ namespace glm
// -- Component accesses -- // -- Component accesses --
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER T & vec<1, T, Q>::operator[](typename vec<1, T, Q>::length_type i) GLM_FUNC_QUALIFIER GLM_CONSTEXPR T & vec<1, T, Q>::operator[](typename vec<1, T, Q>::length_type i)
{ {
assert(i >= 0 && i < this->length()); assert(i >= 0 && i < this->length());
return (&x)[i]; return (&x)[i];
} }
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER T const& vec<1, T, Q>::operator[](typename vec<1, T, Q>::length_type i) const GLM_FUNC_QUALIFIER GLM_CONSTEXPR T const& vec<1, T, Q>::operator[](typename vec<1, T, Q>::length_type i) const
{ {
assert(i >= 0 && i < this->length()); assert(i >= 0 && i < this->length());
return (&x)[i]; return (&x)[i];

View File

@ -60,7 +60,7 @@ namespace glm
typedef length_t length_type; typedef length_t length_type;
GLM_FUNC_DECL static GLM_CONSTEXPR length_type length(){return 2;} GLM_FUNC_DECL static GLM_CONSTEXPR length_type length(){return 2;}
GLM_FUNC_DECL T& operator[](length_type i); GLM_FUNC_DECL GLM_CONSTEXPR T& operator[](length_type i);
GLM_FUNC_DECL GLM_CONSTEXPR T const& operator[](length_type i) const; GLM_FUNC_DECL GLM_CONSTEXPR T const& operator[](length_type i) const;
// -- Implicit basic constructors -- // -- Implicit basic constructors --
@ -114,10 +114,10 @@ namespace glm
// -- Unary arithmetic operators -- // -- Unary arithmetic operators --
GLM_FUNC_DECL vec<2, T, Q> & operator=(vec const& v) GLM_DEFAULT; GLM_FUNC_DECL GLM_CONSTEXPR vec<2, T, Q> & operator=(vec const& v) GLM_DEFAULT;
template<typename U> template<typename U>
GLM_FUNC_DECL vec<2, T, Q> & operator=(vec<2, U, Q> const& v); GLM_FUNC_DECL GLM_CONSTEXPR vec<2, T, Q> & operator=(vec<2, U, Q> const& v);
template<typename U> template<typename U>
GLM_FUNC_DECL vec<2, T, Q> & operator+=(U scalar); GLM_FUNC_DECL vec<2, T, Q> & operator+=(U scalar);
template<typename U> template<typename U>
@ -193,27 +193,27 @@ namespace glm
// -- Unary operators -- // -- Unary operators --
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_DECL vec<2, T, Q> operator+(vec<2, T, Q> const& v); GLM_FUNC_DECL GLM_CONSTEXPR vec<2, T, Q> operator+(vec<2, T, Q> const& v);
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_DECL vec<2, T, Q> operator-(vec<2, T, Q> const& v); GLM_FUNC_DECL GLM_CONSTEXPR vec<2, T, Q> operator-(vec<2, T, Q> const& v);
// -- Binary operators -- // -- Binary operators --
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_DECL vec<2, T, Q> operator+(vec<2, T, Q> const& v, T scalar); GLM_FUNC_DECL GLM_CONSTEXPR vec<2, T, Q> operator+(vec<2, T, Q> const& v, T scalar);
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_DECL vec<2, T, Q> operator+(vec<2, T, Q> const& v1, vec<1, T, Q> const& v2); GLM_FUNC_DECL GLM_CONSTEXPR vec<2, T, Q> operator+(vec<2, T, Q> const& v1, vec<1, T, Q> const& v2);
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_DECL vec<2, T, Q> operator+(T scalar, vec<2, T, Q> const& v); GLM_FUNC_DECL GLM_CONSTEXPR vec<2, T, Q> operator+(T scalar, vec<2, T, Q> const& v);
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_DECL vec<2, T, Q> operator+(vec<1, T, Q> const& v1, vec<2, T, Q> const& v2); GLM_FUNC_DECL GLM_CONSTEXPR vec<2, T, Q> operator+(vec<1, T, Q> const& v1, vec<2, T, Q> const& v2);
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_DECL vec<2, T, Q> operator+(vec<2, T, Q> const& v1, vec<2, T, Q> const& v2); GLM_FUNC_DECL GLM_CONSTEXPR vec<2, T, Q> operator+(vec<2, T, Q> const& v1, vec<2, T, Q> const& v2);
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_DECL vec<2, T, Q> operator-(vec<2, T, Q> const& v, T scalar); GLM_FUNC_DECL vec<2, T, Q> operator-(vec<2, T, Q> const& v, T scalar);

View File

@ -102,7 +102,7 @@ namespace glm
// -- Component accesses -- // -- Component accesses --
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER T & vec<2, T, Q>::operator[](typename vec<2, T, Q>::length_type i) GLM_FUNC_QUALIFIER GLM_CONSTEXPR T & vec<2, T, Q>::operator[](typename vec<2, T, Q>::length_type i)
{ {
assert(i >= 0 && i < this->length()); assert(i >= 0 && i < this->length());
return (&x)[i]; return (&x)[i];
@ -119,7 +119,7 @@ namespace glm
# if GLM_USE_DEFAULTED_FUNCTIONS == GLM_DISABLE # if GLM_USE_DEFAULTED_FUNCTIONS == GLM_DISABLE
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<2, T, Q> & vec<2, T, Q>::operator=(vec<2, T, Q> const& v) GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<2, T, Q> & vec<2, T, Q>::operator=(vec<2, T, Q> const& v)
{ {
this->x = v.x; this->x = v.x;
this->y = v.y; this->y = v.y;
@ -129,7 +129,7 @@ namespace glm
template<typename T, qualifier Q> template<typename T, qualifier Q>
template<typename U> template<typename U>
GLM_FUNC_QUALIFIER vec<2, T, Q> & vec<2, T, Q>::operator=(vec<2, U, Q> const& v) GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<2, T, Q> & vec<2, T, Q>::operator=(vec<2, U, Q> const& v)
{ {
this->x = static_cast<T>(v.x); this->x = static_cast<T>(v.x);
this->y = static_cast<T>(v.y); this->y = static_cast<T>(v.y);
@ -445,13 +445,13 @@ namespace glm
// -- Unary arithmetic operators -- // -- Unary arithmetic operators --
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<2, T, Q> operator+(vec<2, T, Q> const& v) GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<2, T, Q> operator+(vec<2, T, Q> const& v)
{ {
return v; return v;
} }
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<2, T, Q> operator-(vec<2, T, Q> const& v) GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<2, T, Q> operator-(vec<2, T, Q> const& v)
{ {
return vec<2, T, Q>( return vec<2, T, Q>(
-v.x, -v.x,
@ -461,7 +461,7 @@ namespace glm
// -- Binary arithmetic operators -- // -- Binary arithmetic operators --
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<2, T, Q> operator+(vec<2, T, Q> const& v, T scalar) GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<2, T, Q> operator+(vec<2, T, Q> const& v, T scalar)
{ {
return vec<2, T, Q>( return vec<2, T, Q>(
v.x + scalar, v.x + scalar,
@ -469,7 +469,7 @@ namespace glm
} }
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<2, T, Q> operator+(vec<2, T, Q> const& v1, vec<1, T, Q> const& v2) GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<2, T, Q> operator+(vec<2, T, Q> const& v1, vec<1, T, Q> const& v2)
{ {
return vec<2, T, Q>( return vec<2, T, Q>(
v1.x + v2.x, v1.x + v2.x,
@ -477,7 +477,7 @@ namespace glm
} }
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<2, T, Q> operator+(T scalar, vec<2, T, Q> const& v) GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<2, T, Q> operator+(T scalar, vec<2, T, Q> const& v)
{ {
return vec<2, T, Q>( return vec<2, T, Q>(
scalar + v.x, scalar + v.x,
@ -485,7 +485,7 @@ namespace glm
} }
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<2, T, Q> operator+(vec<1, T, Q> const& v1, vec<2, T, Q> const& v2) GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<2, T, Q> operator+(vec<1, T, Q> const& v1, vec<2, T, Q> const& v2)
{ {
return vec<2, T, Q>( return vec<2, T, Q>(
v1.x + v2.x, v1.x + v2.x,
@ -493,7 +493,7 @@ namespace glm
} }
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<2, T, Q> operator+(vec<2, T, Q> const& v1, vec<2, T, Q> const& v2) GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<2, T, Q> operator+(vec<2, T, Q> const& v1, vec<2, T, Q> const& v2)
{ {
return vec<2, T, Q>( return vec<2, T, Q>(
v1.x + v2.x, v1.x + v2.x,

View File

@ -61,7 +61,7 @@ namespace glm
typedef length_t length_type; typedef length_t length_type;
GLM_FUNC_DECL static GLM_CONSTEXPR length_type length(){return 3;} GLM_FUNC_DECL static GLM_CONSTEXPR length_type length(){return 3;}
GLM_FUNC_DECL T & operator[](length_type i); GLM_FUNC_DECL GLM_CONSTEXPR T & operator[](length_type i);
GLM_FUNC_DECL GLM_CONSTEXPR T const& operator[](length_type i) const; GLM_FUNC_DECL GLM_CONSTEXPR T const& operator[](length_type i) const;
// -- Implicit basic constructors -- // -- Implicit basic constructors --

View File

@ -164,7 +164,7 @@ namespace glm
// -- Component accesses -- // -- Component accesses --
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER T & vec<3, T, Q>::operator[](typename vec<3, T, Q>::length_type i) GLM_FUNC_QUALIFIER GLM_CONSTEXPR T & vec<3, T, Q>::operator[](typename vec<3, T, Q>::length_type i)
{ {
assert(i >= 0 && i < this->length()); assert(i >= 0 && i < this->length());
return (&x)[i]; return (&x)[i];

View File

@ -62,7 +62,7 @@ namespace glm
typedef length_t length_type; typedef length_t length_type;
GLM_FUNC_DECL static GLM_CONSTEXPR length_type length(){return 4;} GLM_FUNC_DECL static GLM_CONSTEXPR length_type length(){return 4;}
GLM_FUNC_DECL T & operator[](length_type i); GLM_FUNC_DECL GLM_CONSTEXPR T & operator[](length_type i);
GLM_FUNC_DECL GLM_CONSTEXPR T const& operator[](length_type i) const; GLM_FUNC_DECL GLM_CONSTEXPR T const& operator[](length_type i) const;
// -- Implicit basic constructors -- // -- Implicit basic constructors --

View File

@ -503,7 +503,7 @@ namespace detail
// -- Component accesses -- // -- Component accesses --
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER T& vec<4, T, Q>::operator[](typename vec<4, T, Q>::length_type i) GLM_FUNC_QUALIFIER GLM_CONSTEXPR T& vec<4, T, Q>::operator[](typename vec<4, T, Q>::length_type i)
{ {
assert(i >= 0 && i < this->length()); assert(i >= 0 && i < this->length());
return (&x)[i]; return (&x)[i];

View File

@ -78,8 +78,8 @@ namespace glm
typedef length_t length_type; typedef length_t length_type;
GLM_FUNC_DECL static GLM_CONSTEXPR length_type length(){return 1;} GLM_FUNC_DECL static GLM_CONSTEXPR length_type length(){return 1;}
GLM_FUNC_DECL T & operator[](length_type i); GLM_FUNC_DECL GLM_CONSTEXPR T & operator[](length_type i);
GLM_FUNC_DECL T const& operator[](length_type i) const; GLM_FUNC_DECL GLM_CONSTEXPR T const& operator[](length_type i) const;
// -- Implicit basic constructors -- // -- Implicit basic constructors --

View File

@ -56,7 +56,7 @@ namespace glm
/// Return the count of components of a quaternion /// Return the count of components of a quaternion
GLM_FUNC_DECL static GLM_CONSTEXPR length_type length(){return 4;} GLM_FUNC_DECL static GLM_CONSTEXPR length_type length(){return 4;}
GLM_FUNC_DECL T & operator[](length_type i); GLM_FUNC_DECL GLM_CONSTEXPR T & operator[](length_type i);
GLM_FUNC_DECL GLM_CONSTEXPR T const& operator[](length_type i) const; GLM_FUNC_DECL GLM_CONSTEXPR T const& operator[](length_type i) const;
// -- Implicit basic constructors -- // -- Implicit basic constructors --

View File

@ -75,7 +75,7 @@ namespace detail
// -- Component accesses -- // -- Component accesses --
template<typename T, qualifier Q> template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER T & tquat<T, Q>::operator[](typename tquat<T, Q>::length_type i) GLM_FUNC_QUALIFIER GLM_CONSTEXPR T & tquat<T, Q>::operator[](typename tquat<T, Q>::length_type i)
{ {
assert(i >= 0 && i < this->length()); assert(i >= 0 && i < this->length());
return (&x)[i]; return (&x)[i];

View File

@ -31,6 +31,8 @@ static int test_vec1()
} }
{ {
constexpr glm::ivec1 A(1);
static_assert(A[0] == 1, "GLM: Failed constexpr");
static_assert(glm::vec1(1.0f).x > 0.0f, "GLM: Failed constexpr"); static_assert(glm::vec1(1.0f).x > 0.0f, "GLM: Failed constexpr");
static_assert(glm::vec1::length() == 1, "GLM: Failed constexpr"); static_assert(glm::vec1::length() == 1, "GLM: Failed constexpr");
} }
@ -85,6 +87,8 @@ static int test_vec2()
} }
{ {
constexpr glm::ivec2 A(1);
static_assert(A[0] == 1, "GLM: Failed constexpr");
static_assert(glm::vec2(1.0f).x > 0.0f, "GLM: Failed constexpr"); static_assert(glm::vec2(1.0f).x > 0.0f, "GLM: Failed constexpr");
static_assert(glm::vec2(1.0f, -1.0f).x > 0.0f, "GLM: Failed constexpr"); static_assert(glm::vec2(1.0f, -1.0f).x > 0.0f, "GLM: Failed constexpr");
static_assert(glm::vec2(1.0f, -1.0f).y < 0.0f, "GLM: Failed constexpr"); static_assert(glm::vec2(1.0f, -1.0f).y < 0.0f, "GLM: Failed constexpr");
@ -100,6 +104,13 @@ static int test_vec2()
static_assert(A1 == A2 || B1 == B2, "GLM: Failed constexpr"); static_assert(A1 == A2 || B1 == B2, "GLM: Failed constexpr");
} }
{
constexpr glm::ivec2 A(1);
constexpr glm::ivec2 B = A + 1;
constexpr glm::ivec2 C(3);
static_assert(A + B == C, "GLM: Failed constexpr");
}
return Error; return Error;
} }
@ -161,6 +172,8 @@ static int test_vec3()
} }
{ {
constexpr glm::ivec3 const A(1);
static_assert(A[0] == 1, "GLM: Failed constexpr");
static_assert(glm::vec3(1.0f).x > 0.0f, "GLM: Failed constexpr"); static_assert(glm::vec3(1.0f).x > 0.0f, "GLM: Failed constexpr");
static_assert(glm::vec3(1.0f, -1.0f, -1.0f).x > 0.0f, "GLM: Failed constexpr"); static_assert(glm::vec3(1.0f, -1.0f, -1.0f).x > 0.0f, "GLM: Failed constexpr");
static_assert(glm::vec3(1.0f, -1.0f, -1.0f).y < 0.0f, "GLM: Failed constexpr"); static_assert(glm::vec3(1.0f, -1.0f, -1.0f).y < 0.0f, "GLM: Failed constexpr");
@ -215,6 +228,8 @@ static int test_vec4()
} }
{ {
constexpr glm::ivec4 A(1);
static_assert(A[0] == 1, "GLM: Failed constexpr");
static_assert(glm::ivec4(1).x > 0, "GLM: Failed constexpr"); static_assert(glm::ivec4(1).x > 0, "GLM: Failed constexpr");
static_assert(glm::ivec4(1.0f, -1.0f, -1.0f, 1.0f).x > 0, "GLM: Failed constexpr"); static_assert(glm::ivec4(1.0f, -1.0f, -1.0f, 1.0f).x > 0, "GLM: Failed constexpr");
static_assert(glm::ivec4(1.0f, -1.0f, -1.0f, 1.0f).y < 0, "GLM: Failed constexpr"); static_assert(glm::ivec4(1.0f, -1.0f, -1.0f, 1.0f).y < 0, "GLM: Failed constexpr");
@ -244,6 +259,7 @@ static int test_quat()
glm::quat constexpr Q = glm::identity<glm::quat>(); glm::quat constexpr Q = glm::identity<glm::quat>();
static_assert(Q.x - glm::quat(1.0f, glm::vec3(0.0f)).x <= glm::epsilon<float>(), "GLM: Failed constexpr"); static_assert(Q.x - glm::quat(1.0f, glm::vec3(0.0f)).x <= glm::epsilon<float>(), "GLM: Failed constexpr");
static_assert(Q[0] == 0, "GLM: Failed constexpr");
} }
return Error; return Error;