Created
February 18, 2018 03:38
-
-
Save JochemKuijpers/890118e9749072c75b22b3864a870c7f to your computer and use it in GitHub Desktop.
[CLion] "No matching constructor" but still compiles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "math/Vector.h" | |
int main(int argv, char* args[]) { | |
Vector2i a({1, 2}); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SDL_PROJECT_VECTOR_H | |
#define SDL_PROJECT_VECTOR_H | |
#include <iostream> | |
#include <cstdarg> | |
#include <sstream> | |
#include <cmath> | |
template <typename NumType, int N> | |
class Vector { | |
private: | |
NumType elements[N]; | |
public: | |
Vector(); | |
Vector(const Vector<NumType, N>& other); | |
Vector(const NumType (& values)[N]); | |
~Vector(); | |
NumType& x(); | |
NumType& w(); | |
NumType& y(); | |
NumType& h(); | |
NumType& z(); | |
Vector<NumType, N> operator+(Vector<NumType, N> other); | |
Vector<NumType, N> operator-(Vector<NumType, N> other); | |
Vector<NumType, N> operator*(NumType scalar); | |
Vector<NumType, N> operator/(NumType scalar); | |
NumType operator[](int i) const; | |
NumType& operator[](int i); | |
double lengthSqr(); | |
double length(); | |
std::string toString(); | |
}; | |
template<typename NumType, int N> | |
Vector<NumType, N>::Vector() = default; | |
template<typename NumType, int N> | |
Vector<NumType, N>::Vector(const NumType (& values)[N]) { | |
std::move(&values[0], &values[0] + N, this->elements); | |
} | |
template<typename NumType, int N> | |
Vector<NumType, N>::Vector(const Vector<NumType, N>& other) { | |
for (int i = 0; i < N; ++i) { | |
elements[i] = other.elements[i]; | |
} | |
}; | |
template<typename NumType, int N> | |
Vector<NumType, N>::~Vector() = default; | |
template<typename NumType, int N> | |
NumType &Vector<NumType, N>::x() { | |
return elements[0]; | |
} | |
template<typename NumType, int N> | |
NumType &Vector<NumType, N>::w() { | |
return elements[0]; | |
} | |
template<typename NumType, int N> | |
NumType &Vector<NumType, N>::y() { | |
if (N >= 1) return elements[1]; | |
throw std::runtime_error("y() not defined for this type"); | |
} | |
template<typename NumType, int N> | |
NumType &Vector<NumType, N>::h() { | |
if (N >= 1) return elements[1]; | |
throw std::runtime_error("h() not defined for this type"); | |
} | |
template<typename NumType, int N> | |
NumType &Vector<NumType, N>::z() { | |
if (N >= 2) return elements[2]; | |
throw std::runtime_error("z() not defined for this type"); | |
} | |
template<typename NumType, int N> | |
Vector<NumType, N> Vector<NumType, N>::operator+(Vector<NumType, N> other) { | |
Vector<NumType, N> result; | |
for (int n = 0; n < N; n ++) { | |
result.elements[n] = elements[n] + other.elements[n]; | |
} | |
return result; | |
} | |
template<typename NumType, int N> | |
Vector<NumType, N> Vector<NumType, N>::operator-(Vector<NumType, N> other) { | |
Vector<NumType, N> result; | |
for (int n = 0; n < N; n ++) { | |
result.elements[n] = elements[n] - other.elements[n]; | |
} | |
return result; | |
} | |
template<typename NumType, int N> | |
Vector<NumType, N> Vector<NumType, N>::operator*(NumType scalar) { | |
Vector<NumType, N> result; | |
for (int n = 0; n < N; n ++) { | |
result.elements[n] = elements[n] * scalar; | |
} | |
return result; | |
} | |
template<typename NumType, int N> | |
Vector<NumType, N> Vector<NumType, N>::operator/(NumType scalar) { | |
Vector<NumType, N> result; | |
for (int n = 0; n < N; n ++) { | |
result.elements[n] = elements[n] / scalar; | |
} | |
return result;} | |
template<typename NumType, int N> | |
NumType Vector<NumType, N>::operator[](int i) const { | |
if (0 <= i && i < N) return elements[i]; | |
throw std::invalid_argument("index out of bounds"); | |
} | |
template<typename NumType, int N> | |
NumType &Vector<NumType, N>::operator[](int i) { | |
if (0 <= i && i < N) return elements[i]; | |
throw std::invalid_argument("index out of bounds"); | |
} | |
template<typename NumType, int N> | |
double Vector<NumType, N>::lengthSqr() { | |
double result = 0; | |
for (int n = 0; n < N; n ++) { | |
result += elements[n] * elements[n]; | |
} | |
return result; | |
} | |
template<typename NumType, int N> | |
double Vector<NumType, N>::length() { | |
return sqrt(lengthSqr()); | |
} | |
template<typename NumType, int N> | |
std::string Vector<NumType, N>::toString() { | |
std::stringstream ss; | |
ss << "{" << elements[0]; | |
for (int n = 1; n < N; n++) { | |
ss << ", " << elements[n]; | |
} | |
ss << "}"; | |
return ss.str(); | |
} | |
typedef Vector<int, 2> Vector2i; | |
typedef Vector<long, 2> Vector2l; | |
typedef Vector<float, 2> Vector2f; | |
typedef Vector<double, 2> Vector2d; | |
typedef Vector<int, 3> Vector3i; | |
typedef Vector<long, 3> Vector3l; | |
typedef Vector<float, 3> Vector3f; | |
typedef Vector<double, 3> Vector3d; | |
#endif //SDL_PROJECT_VECTOR_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment