FR: Support structured binding with JUCE types

If I have a function like juce::Range<float> getRange() it would be nice to be able to do:

auto [start, end] = getRange();

I think all that’s required for that is to make the elements public, right?

I like it. But for the time being, you can also maybe use this workaround:

template <typename T>
struct TempRange
    TempRange(const juce::Range<T>& range)
        : start(range.getStart(), end(range.getEnd()))

    T start, end;

juce::Range<float> getJUCERange();
TempRange<float> getRange()
    return getJUCERange();

int main()
    auto[start, end] = getRange();
    return start;

Adding structured binding support for arbitrary custom types works by supplying template specialisations for std::tuple_size, std::tuple_element and std::get. This blog entry describes it in detail.

Since these are free functions, you can just implement them yourself in your code without needing to wait for JUCE to implement it.

Still I support this request, I think there are some types that could benefit from it. By the way, dsp::ProcessorChain already supports it, which is quite handy, I don‘t know if there are already further JUCE types with structured binding support?


See Also: < Code Style Request: More public members, pls - #65 by nicolasdanet >

1 Like