Since Array returns const& in foreach loops the later one is broken.
Removing the & fixed it for me.
bool getIntersectionWith (RectangleType rect, RectangleList& destRegion) const
{
jassert (rect.isFinite()); // You must provide a valid rectangle to this method!
destRegion.clear();
if (! rect.isEmpty())
for (auto**&** r : rects)
if (rect.intersectRectangle (r))
destRegion.rects.add (r);
return ! destRegion.isEmpty();
}
In my project I have something like this boiled down code:
RectangleList< int > rectListSource, rectListTarget;
rectListSource.getIntersectionWith (Rectangle< int > {100, 100}, rectListTarget);
and it leads to compile errors as follows.
You could reproduce this if you paste the code into the demo runner project as well.
In file included from ***Main.cpp:27:
In file included from …/…/JuceLibraryCode/…/JuceLibraryCode/JuceHeader.h:17:
In file included from …/…/…/…/modules/juce_analytics/juce_analytics.h:55:
In file included from …/…/…/…/modules/juce_gui_basics/juce_gui_basics.h:57:
In file included from …/…/…/…/modules/juce_graphics/juce_graphics.h:120:
…/…/…/…/modules/juce_graphics/geometry/juce_RectangleList.h:390:26: error: no matching member function for call to ‘intersectRectangle’
if (rect.intersectRectangle ®)
~^~~~~~~~~~~~~~
***Main.cpp:149:28: note: in instantiation of member function ‘juce::RectangleList::getIntersectionWith’ requested here
rectListSource.getIntersectionWith(Rectangle {100, 100}, rectListTarget);
^
In file included from ***Main.cpp:27:
In file included from …/…/JuceLibraryCode/…/JuceLibraryCode/JuceHeader.h:17:
In file included from …/…/…/…/modules/juce_analytics/juce_analytics.h:55:
In file included from …/…/…/…/modules/juce_gui_basics/juce_gui_basics.h:57:
In file included from …/…/…/…/modules/juce_graphics/juce_graphics.h:116:
…/…/…/…/modules/juce_graphics/geometry/juce_Rectangle.h:681:10: note: candidate function not viable: 1st argument (‘const juce::Rectangle’) would lose const qualifier
bool intersectRectangle (Rectangle& rectangleToClip) const noexcept
^
…/…/…/…/modules/juce_graphics/geometry/juce_Rectangle.h:658:10: note: candidate function not viable: requires 4 arguments, but 1 was provided
bool intersectRectangle (ValueType& otherX, ValueType& otherY, ValueType& otherW, ValueType& otherH) const noexcept
Wouldn’t it be better to make const the Rectangle& parameter received by intersectRectangle, rather than transforming it into a pass by copy?
I understand the logic of passing Points by copy because they only have 2 values, but Rectangles have 4 and I was under the impression that the JUCE API preferred by-reference for those.
This will not work because Rectangle< ValueType >::intersectRectangle ( Rectangle< ValueType > &rectangleToClip) expects a non-const reference that gets modified internally.
You can pass surprisingly large POD types before passing a reference is quicker. When there’s a reference it’s possible that the code inside the function could offset off the memory address and poke around in other bits of the codebase. If everything is a copy then the optimiser can be much more aggressive. But in either case this fix was an embarrassing error due to our CI being offline all day and there’s a fix for the fix on the way.
I’d love to have an easy tool to benchmark similar behaviours regularly, e.g. on different CPUs and/or when a new version of the build tools comes out.
I’m not doubting what you say, I mean it in the sense: “I’d like to make informed design decisions as technology advances” rather than relying on “rumors”
It’s useful for inspecting these kinds of things, with the significant danger that microbenchmarks don’t always translate well when used in chunks of more complicated code.