coord/base: add std::array conversion operator

This commit is contained in:
Danny Robson 2018-06-12 10:54:08 +10:00
parent 0a599b4b14
commit 22b25221b2
2 changed files with 21 additions and 0 deletions

View File

@ -25,6 +25,7 @@
#include "../maths.hpp" #include "../maths.hpp"
#include <algorithm> #include <algorithm>
#include <array>
#include <cstdlib> #include <cstdlib>
#include <type_traits> #include <type_traits>
@ -104,6 +105,7 @@ namespace util::coord {
const T& back (void) const { return this->data[S-1]; } const T& back (void) const { return this->data[S-1]; }
T& back (void) { return this->data[S-1]; } T& back (void) { return this->data[S-1]; }
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
// conversions // conversions
template <template <std::size_t, typename> class K> template <template <std::size_t, typename> class K>
@ -142,6 +144,16 @@ namespace util::coord {
return out; return out;
} }
///////////////////////////////////////////////////////////////////////
operator std::array<value_type,elements> () const
{
std::array<value_type,elements> res;
std::copy (begin (), end (), std::begin (res));
return res;
}
/////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////
/// returns an instance with the same data, but truncated to `D' /// returns an instance with the same data, but truncated to `D'
/// elements /// elements

View File

@ -42,6 +42,15 @@ main (void)
tap.expect (x == p.x && y == p.y, "structured bindings extract correct data"); tap.expect (x == p.x && y == p.y, "structured bindings extract correct data");
} }
// check that we can assign to arrays from coord types
{
std::array<int,2> q = p;
tap.expect (
std::equal (std::begin (q), std::end (q), std::begin (p)),
"assignment to array preserves values"
);
}
// ensure the distance function behaves correctly with non-normal numbers. // ensure the distance function behaves correctly with non-normal numbers.
{ {
util::point3f a { 103, 0, 14 }; util::point3f a { 103, 0, 14 };