11 // From: "Demo Zoo: Zero Cost Abstractions in C++20, Rust, & Zig": https://www.youtube.com/watch?v=43X9ia-qpds
16 using Scalar
= std::decay_t
<decltype(Vec()[0])>;
20 std::floating_point
<Scalar
<Vec
>> &&
23 { vec
.size() } -> std::integral
;
26 template<FloatVec Vec
>
27 auto norm(Vec vec
) -> Scalar
<Vec
> // Should be 'norm(const Vec& vec)' but it doesn't work with VS 2019 16.6.3.
29 using Index
= decltype(vec
.size());
30 Scalar
<Vec
> result
= 0;
31 for (Index i
= 0; i
< vec
.size(); i
+= 1)
33 result
+= vec
[i
] * vec
[i
];
35 return std::sqrt(result
);
47 auto operator[](int i
) const -> float {
48 return i
== 0 ? x
: y
;
52 float norm2(float x
, float y
)
54 return norm(Point2
{ x
, y
});
57 double norm_nd(const std::vector
<double>& a
)
63 void Concepts::tests()
65 cout
<< "norm: " << norm_nd({ 1, 2, 3 }) << endl
;
66 cout
<< "norm: " << norm2(3, 4) << endl
;