so this basically makes some sort of array of type T, but without having to access the array with an index, but actually by its member’s name, right? really interesting concept. i never saw this before
Not quite. Structured bindings allow you to “bind” public members of a structure to a corresponding number of variables.
The variables don’t have to have the same name, so you could have
const auto [width, height] = unpack (bounds);
// or
const auto [w, h] = unpack (bounds);
// or
const auto [length, thickness] = unpack (bounds);
It doesn’t make an array, and the type of the values depends on the types in the structure, hence the importance of using auto
.
You can also get references to the members using:
auto& [a, b] = unpack (bounds);
awesome. yeah sounds like that could actually speed up quite some GUI code writing and the only cost is that the reader must know this c++ feature
The drawback is that you must know the order of the members in the tuple, hence I would always prefer a struct where the members have names.
So we are back to the original Rectangle, which is the solution I like best.
IMHO it is not broken, no need to fix it.
@daniel I’m agree, this is a serious drawback (as for std::Tuple).
@PluginPenguin Thanks! I didn’t know that it could be done without modify the JUCE source.
I did a quick test for fun.
namespace std {
template <> struct tuple_size<juce::Rectangle<int>> : integral_constant<size_t, 2> {};
template <> struct tuple_element<0, juce::Rectangle<int>> { using type = int; };
template <> struct tuple_element<1, juce::Rectangle<int>> { using type = int; };
}
namespace juce {
template<std::size_t Index> std::tuple_element_t<Index, juce::Rectangle<int>> get (const juce::Rectangle<int>& r)
{
if constexpr (Index == 0) return r.getWidth();
if constexpr (Index == 1) return r.getHeight();
}
}
void MainComponent::resized()
{
const auto [w, h] = getLocalBounds();
DBG (w);
DBG (h);
}
And it seems to work perfectly fine.