# Structured binding of StringPairArray

**URL:** <https://forum.juce.com/t/structured-binding-of-stringpairarray/58083>\
**Category:** Feature Requests\
**Created:** [September 28, 2023, 5:50pm UTC](https://forum.juce.com/t/structured-binding-of-stringpairarray/58083 "2023-09-28T17:50:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdamVenn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/adamvenn/32/17023_2.png) [@AdamVenn](https://forum.juce.com/u/AdamVenn)\
**Post date:** [September 28, 2023, 5:50pm UTC](https://forum.juce.com/t/structured-binding-of-stringpairarray/58083/1 "2023-09-28T17:50:55Z")

</div>

This would be nice:

```auto
void processOptions(StringPairArray options)
{
    for (auto& [key, val] : options)
    {
        applyOption(key, val);
    }
}

```

---

<div class="post-metadata">

**Author:** ![AdamVenn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/adamvenn/32/17023_2.png) [@AdamVenn](https://forum.juce.com/u/AdamVenn)\
**Post date:** [September 28, 2023, 6:26pm UTC](https://forum.juce.com/t/structured-binding-of-stringpairarray/58083/2 "2023-09-28T18:26:54Z")

</div>

This is new to me, but I tried the following, based on [this post](https://forum.juce.com/t/fr-support-structured-binding-with-juce-types/52285/3) but I get a `this range-based 'for' statement requires a suitable "begin" function and none was found`. ☹

```auto
template<>
struct std::tuple_size<StringPairArray>
    : std::integral_constant<std::size_t, 2> {};

template<> struct std::tuple_element<0, StringPairArray> { using type = StringRef; };
template<> struct std::tuple_element<1, StringPairArray> { using type = StringRef; };

```

---

<div class="post-metadata">

**Author:** ![AdamVenn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/adamvenn/32/17023_2.png) [@AdamVenn](https://forum.juce.com/u/AdamVenn)\
**Post date:** [September 28, 2023, 6:30pm UTC](https://forum.juce.com/t/structured-binding-of-stringpairarray/58083/3 "2023-09-28T18:30:02Z")

</div>

So I’m going with:

```auto
    for (auto& key : options.getAllKeys())
    {
        auto& val = options[key];
        applyOption(key, val);
    }

```

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [September 29, 2023, 7:51am UTC](https://forum.juce.com/t/structured-binding-of-stringpairarray/58083/4 "2023-09-29T07:51:32Z")

</div>

The problem is that `juce::StringPairArray` does not have a `begin` and `end` member function. In cases like this you can still enable range-based-for loop support if you supply `begin` and `end` functions as free functions. These functions should return a suitable iterator class. A `juce::StringPairArray` stores keys and values in two individual `juce::StringArray` members, so this custom iterator class needs to hold iterators to both arrays, increase both iterators simultaneously in `operator++`. Dereferencing that iterator should return a struct with two references to the current key and value. If a simple struct like that is returned, we don’t even need to do anything special to enable structured bindings.

I just whipped up a quick proof of concept implementation like this:

```auto
// Forward declarations
class StringPairIterator;

namespace juce
{
StringPairIterator begin (const juce::StringPairArray& spa);
StringPairIterator end (const juce::StringPairArray& spa);
}

struct StringPairRef
{
    const juce::String& key;
    const juce::String& value;
};

class StringPairIterator
{
public:
    StringPairIterator& operator++()
    {
        ++key;
        ++value;
        return *this;
    }

    StringPairRef operator*()
    {
        return { *key, *value };
    }

    bool operator== (const StringPairIterator& other) const
    {
        return key == other.key;
    }

private:
    friend StringPairIterator juce::begin (const juce::StringPairArray&);
    friend StringPairIterator juce::end (const juce::StringPairArray&);

    StringPairIterator (const juce::String* k, const juce::String* v)
        : key (k),
          value (v)
    {}

    const juce::String* key;
    const juce::String* value;
};

namespace juce
{
StringPairIterator begin (const juce::StringPairArray& spa)
{
    return { spa.getAllKeys().begin(), spa.getAllValues().begin() };
}

StringPairIterator end (const juce::StringPairArray& spa)
{
    return { spa.getAllKeys().end(), spa.getAllValues().end() };
}
}

```

With that, this works fine for me:

```auto
void processOptions (juce::StringPairArray options)
{
    for (const auto& [key, val] : options)
    {
        applyOption (key, val);
    }
}

```

One note: Since `StringPairArray` is implemented in the `juce` namespace, the `begin` and `end` functions have to be implemented in the same namespace to make the lookup of the correct overload work.

---

<div class="post-metadata">

**Author:** ![AdamVenn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/adamvenn/32/17023_2.png) [@AdamVenn](https://forum.juce.com/u/AdamVenn)\
**Post date:** [September 29, 2023, 10:49pm UTC](https://forum.juce.com/t/structured-binding-of-stringpairarray/58083/5 "2023-09-29T22:49:30Z")

</div>

Thank you, Sir Penguin! Most educational.
