Geom::OptRect and "warning: dereferencing pointer ‘<anonymous>’ does break strict-aliasing rules"
I get this warning everywhere an OptRect is used. For example:
SPItem *item; Geom::OptRect r = item->getBounds(sp_item_i2doc_affine(item)); if (r) { Geom::Point p = r->min(); //...
will give the warning "./2geom/interval.h:82: warning: dereferencing pointer ‘<anonymous>’ does break strict-aliasing rules".
To fix this, shouldn't getBounds return optionalGeom::Rect? Making an OptRect class seems the wrong use of boost::optional. To remove OptRect: isEmpty() becomes "if (optional<Rect> r)". unionWith(OptRect const &b) becomes another method in Geom::Rect, unionWith(optional<Rect> b).
See http://www.boost.org/doc/libs/1_41_0/libs/optional/doc/html/boost_optional/e...
I'm willing to refactor to remove OptRect. Any objections?
- Alex
2010/1/10 Alex Leone <acleone@...400...>:
I'm willing to refactor to remove OptRect. Any objections?
OptRect exists for a reason. For example it is the prefect result type for getting a bounding box of a possibly empty set of points. Compare this:
Geom::OptRect bounds; for (int i = 0; i < n; ++i) { bounds.unionWith(Geom::OptRect(points[i], points[i]); }
to this:
Geom::Rect bounds; bool has_bounds = false; for (int i = 0; i < n; ++i) { if (!has_bounds) { has_bounds = true; bounds = Geom::Rect(points[i], points[i]); } else { bounds.expandTo(points[i]); } }
The methods might have slighty different names, but you get the idea. It's immensely useful to have two types of rectangle: one that can be empty (OptRect) and one that can't. This way you can avoid a lot of redundant error checking in functions that expect a non-empty rectangle.
I think the aliasing warnings can be fixed by tweaking the definition of OptRect a bit, because they did not appear before.
Regards, Krzysztof
In fact it is a bug in boost::optional (or in the strict aliasing rules for C, depending on which way you look at it). http://lists.boost.org/Archives/boost/2009/05/151318.php
-fno-strict-aliasing?
Regards, Krzysztof
Or it might be a bug in GCC, which issues a spurious warning for a case when aliasing rules are not broken. http://lists.boost.org/Archives/boost/2009/05/151577.php
Regards, Krzysztof
Strangely enough, if I replace
(Geom::OptRect) r->min()
with
r.get_ptr()->min()
the warning goes away. Also the compiled binary size decreases by 200 bytes.
- Alex
participants (3)
-
Alex Leone
-
Diederik van Lierop
-
Krzysztof Kosiński