Skip to main content
Rollback to Revision 4
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

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

#include <iostream>
#include <vector>
#include <set>

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

template <typename CONTAINER>
CONTAINER enumToSTLContainerEnumToSTLContainer (typename CONTAINER::value_type first, typename CONTAINER::value_type last) {
    CONTAINER v;
    for (typename std::underlying_type<typename CONTAINER::value_type>::typeint N = first;  N <= last;  N++)
        v.push_back(static_cast<typename CONTAINER::value_type>(N));  // But only works for STL containers with push_back defined.
    return v;
}

int main() {
    const std::vector<MyEnum> enumVector = enumToSTLContainer<stdEnumToSTLContainer<std::vector<MyEnum>> (FirstMyEnum, LastMyEnum);
    for (MyEnum x : enumVector)
        std::cout << x << ' ';  // 0 1 2 3 4
//  const std::set<MyEnum> enumSet = enumToSTLContainer<stdEnumToSTLContainer<std::set<MyEnum>> (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?

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 <iostream>
#include <vector>
#include <set>

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

template <typename CONTAINER>
CONTAINER enumToSTLContainer (typename CONTAINER::value_type first, typename CONTAINER::value_type last) {
    CONTAINER v;
    for (typename std::underlying_type<typename CONTAINER::value_type>::type N = first;  N <= last;  N++)
        v.push_back(static_cast<typename CONTAINER::value_type>(N));  // But only works for STL containers with push_back defined.
    return v;
}

int main() {
    const std::vector<MyEnum> enumVector = enumToSTLContainer<std::vector<MyEnum>> (FirstMyEnum, LastMyEnum);
    for (MyEnum x : enumVector)
        std::cout << x << ' ';  // 0 1 2 3 4
//  const std::set<MyEnum> enumSet = enumToSTLContainer<std::set<MyEnum>> (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?

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 <iostream>
#include <vector>
#include <set>

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

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

int main() {
    const std::vector<MyEnum> enumVector = EnumToSTLContainer<std::vector<MyEnum>> (FirstMyEnum, LastMyEnum);
    for (MyEnum x : enumVector)
        std::cout << x << ' ';  // 0 1 2 3 4
//  const std::set<MyEnum> enumSet = EnumToSTLContainer<std::set<MyEnum>> (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?

added 64 characters in body
Source Link
prestokeys
  • 1.4k
  • 10
  • 19

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

#include <iostream>
#include <vector>
#include <set>

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

template <typename CONTAINER>
CONTAINER EnumToSTLContainerenumToSTLContainer (typename CONTAINER::value_type first, typename CONTAINER::value_type last) {
    CONTAINER v;
    for (inttypename std::underlying_type<typename CONTAINER::value_type>::type N = first;  N <= last;  N++)
        v.push_back(static_cast<typename CONTAINER::value_type>(N));  // But only works for STL containers with push_back defined.
    return v;
}

int main() {
    const std::vector<MyEnum> enumVector = EnumToSTLContainer<stdenumToSTLContainer<std::vector<MyEnum>> (FirstMyEnum, LastMyEnum);
    for (MyEnum x : enumVector)
        std::cout << x << ' ';  // 0 1 2 3 4
//  const std::set<MyEnum> enumSet = EnumToSTLContainer<stdenumToSTLContainer<std::set<MyEnum>> (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?

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 <iostream>
#include <vector>
#include <set>

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

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

int main() {
    const std::vector<MyEnum> enumVector = EnumToSTLContainer<std::vector<MyEnum>> (FirstMyEnum, LastMyEnum);
    for (MyEnum x : enumVector)
        std::cout << x << ' ';  // 0 1 2 3 4
//  const std::set<MyEnum> enumSet = EnumToSTLContainer<std::set<MyEnum>> (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?

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 <iostream>
#include <vector>
#include <set>

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

template <typename CONTAINER>
CONTAINER enumToSTLContainer (typename CONTAINER::value_type first, typename CONTAINER::value_type last) {
    CONTAINER v;
    for (typename std::underlying_type<typename CONTAINER::value_type>::type N = first;  N <= last;  N++)
        v.push_back(static_cast<typename CONTAINER::value_type>(N));  // But only works for STL containers with push_back defined.
    return v;
}

int main() {
    const std::vector<MyEnum> enumVector = enumToSTLContainer<std::vector<MyEnum>> (FirstMyEnum, LastMyEnum);
    for (MyEnum x : enumVector)
        std::cout << x << ' ';  // 0 1 2 3 4
//  const std::set<MyEnum> enumSet = enumToSTLContainer<std::set<MyEnum>> (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?

added 6 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

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

#include <iostream>
#include <vector>
#include <set>

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

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

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

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

Perhaps obtainobtaining an initializer_listinitializer_list using variadic templates would work, which can then return the desired 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 <iostream>
#include <vector>
#include <set>

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

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

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

Problems with the above. Only works for STL containers with push_back defined, need to specify first and last element of the enum list, does not work if enum values are not consecutive, ...

Perhaps obtain an initializer_list using variadic templates, which can then return the desired 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 <iostream>
#include <vector>
#include <set>

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

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

int main() {
    const std::vector<MyEnum> enumVector = EnumToSTLContainer<std::vector<MyEnum>> (FirstMyEnum, LastMyEnum);
    for (MyEnum x : enumVector)
        std::cout << x << ' ';  // 0 1 2 3 4
//  const std::set<MyEnum> enumSet = EnumToSTLContainer<std::set<MyEnum>> (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?

added 113 characters in body
Source Link
prestokeys
  • 1.4k
  • 10
  • 19
Loading
added 15 characters in body
Source Link
prestokeys
  • 1.4k
  • 10
  • 19
Loading
Source Link
prestokeys
  • 1.4k
  • 10
  • 19
Loading