C++Guns – RoboBlog

17.05.2018

C++ Guns: Point Concept

Filed under: Allgemein — Tags: — Thomas @ 11:05

Ich hab mich mal schnell in Concepts eingearbeitet und provisorisch die std library concepts implementiert. Darauf hin habe ich mir ein Concept eines PointND überlegt. Nun das müsste einfach nur ein Container sein, mit den Funktionen x(), y() und Operatoren für +, -, *, /.

So wurde aus dem Stück Code mit viel Templates:

template<typename Array>
inline auto operator+(const PointND<Array>& lhs, const PointND<Array>& rhs) {
    return PointND<Array>{static_cast<Array>(lhs) + static_cast<Array>(rhs)};
}

dieses Stück:

template<typename PointND>
concept bool Point =
        requires(PointND p1, PointND p2) {
        typename PointND::Base;
        requires std::Container<PointND>;
{p1.x()} -> typename PointND::const_reference;
{p1.y()} -> typename PointND::const_reference;
{p1+p2} -> PointND;
{p1-p2} -> PointND;
{p1*p2} -> PointND;
{p1/p2} -> PointND;
};

template<Point PointND>
inline auto operator+(const PointND& lhs, const PointND& rhs) {
    return PointND{static_cast<typename PointND::Base>(lhs) + static_cast<typename PointND::Base>(rhs)};
}

Na das ist doch schon besser.

https://sourceforge.net/p/acpl/code/ci/master/tree/acpl/acpllib/include/core/stdconcepts.hpp
https://sourceforge.net/p/acpl/code/ci/master/tree/acpl/acpllib/include/core/geometry/point.hpp

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress