Given an enum, is there a semi-automated way of returning a vector of all 
named enum values? The best I can come up with is this:

#include<iostream>
#include <vector>

template <typename Enum>
std::vector<Enum> const all_values_if_consecutive(Enum first, Enum last)
{
        std::vector<Enum> vals(last - first + 1);
        typename std::vector<Enum>::iterator it  = vals.begin();
        typename std::vector<Enum>::iterator end = vals.end();

        int value = first;
        for (; it != end; ++it, ++value)
                *it = Enum(value);

        return vals;
}


enum TransformID {
        Rotate,
        Resize,
        Clip,
        Extra
};

int main()
{
        std::vector<TransformID> const ids =
                all_values_if_consecutive(Rotate, Extra);

        std::vector<TransformID>::const_iterator it  = ids.begin();
        std::vector<TransformID>::const_iterator end = ids.end();
        for (; it != end; ++it)
                std::cout << *it << ' ';
        std::cout << std::endl;

        return 0;
}

-- 
Angus

Reply via email to