Enum list to STL container Can someone improve upon this function of mine?  EnumToSTLContainer is supposed to make a copy of any STL container whose elements are from a specified enum list.

#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;set&gt;

enum MyEnum {A, B, C, D, E, FirstMyEnum = A, LastMyEnum = E};

template &lt;typename CONTAINER&gt;
CONTAINER EnumToSTLContainer (typename CONTAINER::value_type first, typename CONTAINER::value_type last) {
    CONTAINER v;
    for (int N = first;  N &lt;= last;  N++)
        v.push_back(static_cast&lt;typename CONTAINER::value_type&gt;(N));  // But only works for STL containers with push_back defined.
    return v;
}

int main() {
    const std::vector&lt;MyEnum&gt; enumVector = EnumToSTLContainer&lt;std::vector&lt;MyEnum&gt;&gt; (FirstMyEnum, LastMyEnum);
    for (MyEnum x : enumVector)
        std::cout &lt;&lt; x &lt;&lt; ' ';  // 0 1 2 3 4
//  const std::set&lt;MyEnum&gt; enumSet = EnumToSTLContainer&lt;std::set&lt;MyEnum&gt;&gt; (FirstMyEnum, LastMyEnum);  // won't compile
}


This only works for STL containers with push_back() defined, and need to specify first and last element of the enum list. It does not work if enum values are not consecutive.

Perhaps obtaining an initializer_list using variadic templates would work, which can then return the desired STL container?
First of all, you should use std::underlying_type_t&lt;Cont::value_type&gt; instead of int and please don't use UPPER for template parameters (UPPER is mostly for preprocessor, sometimes used for constants and/or static variables), rather use PascalCasing, e.g. Container.

Your implementation looks good to me otherwise, but you know it can only be used with containers with member push_back. If you want to create it from initializer_list, then it would be something different (e.g. EnumRangeInitList). You may find some inspiration in std::integer_sequence.