How to get the first/only element in TSet

If I only have one element in the set I would like to access it. Calling Array() and then using the [] operator seems like the only way to do this, and it also seems like overkill. Is there a better way?

Hi sgp,

It’s kind of going against the spirit of a TSet to use it like a TOptional or to ‘index’ into it directly, which is why there is no direct support in the API for it. However, you could use something like this:

template <typename SetType>
typename TCopyQualifiersFromTo<SetType, typename SetType::ElementType>::Type* GetFirstSetElement(SetType& Set)
{
    for (auto& Element : Set)
    {
        return &Element;
    }
    return nullptr;
}

void Func(const TSet<T>& MySet)
{
    if (const T* Val = GetFirstSetElement(MySet))
    {
        // MySet has at least one element, and Val is pointing to one of them
    }
}

Hope this helps,

Steve