{"id":2748,"date":"2017-03-01T15:38:53","date_gmt":"2017-03-01T14:38:53","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=2748"},"modified":"2017-03-01T19:00:36","modified_gmt":"2017-03-01T18:00:36","slug":"point3d-ist-keine-erweiterung-von-point2d","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=2748","title":{"rendered":"C++ Guns - Point3D ist KEINE Erweiterung von Point2D!"},"content":{"rendered":"<p>Point3D ist KEINE Erweiterung von Point2D!<br \/>\nSondern Point2D ist eine Reduzierung von Point3D. WTF? Ja ist so :P<\/p>\n<p>Beweis:<\/p>\n<pre><code>\r\nstruct Point2D {\r\n    double x,y;\r\n};\r\n\r\nstruct Point3D : public Point2D {\r\n    double z;\r\n};\r\n\r\nstatic_assert(std::is_standard_layout&ltPoint3D>::value);\r\n<\/code><\/pre>\n<pre>\r\nmain.cpp:14:1: error: static assertion failed\r\nstatic_assert(std::is_standard_layout&ltPoint3D>::value);\r\n<\/pre>\n<p>In der C++ Sprache ist alles, was berechnet werden kann, ausdr\u00fcckbar. Wird dabei das Standard Layout nicht eingehalten, ist definitiv der falsche Weg eingeschlagen worden.<\/p>\n<p>Der zweite Tipp ist, dass ohne Standardlayout die Typen auch nicht POD sind. Und man kann wohl getrost annehmen, wenn ein double POD ist. Dann muss auch ein Type mit 2 oder 3 double POD sein!<\/p>\n<p>L\u00f6sung:<br \/>\nPoint2D ist auch im Point3D enthalten. Es wird halt nicht x,y,z beachtet, sondern nur x,y.<br \/>\nWenn PointND als Array implementiert wird, ist das nichts anderes als array_view (string_view).<br \/>\nSprich, wird der Datensatz als Point3D gespeichert (owning), sind Point2D und Point1D non-owning.<\/p>\n<p>Beispiel:<\/p>\n<pre><code>\r\ntemplate&ltsize_t N>\r\nstruct PointNDRef : public array_view&ltdouble, N>\r\n{\r\n    using array_view&ltdouble,N>::array_view;\r\n};\r\n\r\ntemplate&ltsize_t N>\r\nstruct PointND : public std::array&ltdouble,N>\r\n{\r\n    operator PointNDRef&lt2>() {\r\n        return this->data();\r\n    }\r\n\r\n    operator PointNDRef&lt1>() {\r\n        return this->data();\r\n    }\r\n};\r\n\r\nstatic_assert(std::is_standard_layout&ltPointND>::value);\r\n\r\nusing Point3D = PointND&lt3>;\r\nusing Point2DRef = PointNDRef&lt2>;\r\n\r\nvoid func(const Point2DRef p) {\r\n    p[0];\r\n    get&lt0>(p);\r\n}\r\n\r\nint main() {\r\n    Point3D p;\r\n    func(p);\r\n    return 0;\r\n}\r\n<\/code><\/pre>\n<p>Die Implementation von array_view<N> kommt dann irgendwann die Wochen.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Point3D ist KEINE Erweiterung von Point2D! Sondern Point2D ist eine Reduzierung von Point3D. WTF? Ja ist so :P Beweis: struct Point2D { double x,y; }; struct Point3D : public Point2D { double z; }; static_assert(std::is_standard_layout&#038;ltPoint3D>::value); main.cpp:14:1: error: static assertion failed static_assert(std::is_standard_layout&#038;ltPoint3D>::value); In der C++ Sprache ist alles, was berechnet werden kann, ausdr\u00fcckbar. Wird dabei das [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[17],"class_list":["post-2748","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-cpp"],"_links":{"self":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2748","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2748"}],"version-history":[{"count":5,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2748\/revisions"}],"predecessor-version":[{"id":2756,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2748\/revisions\/2756"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2748"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}