templatesenumsc++14template-argument-deductionnon-type

User specified non-type template parameter dependent on deduced template


I have an array-like class with non-type enum accessors.

template <class T, class Enum, size_t N>
struct enum_array {
    template <Enum E>
    void do_fancy_thing_with_non_type();
};

I am having issues implementing a get function (it is in a seperate namespace) since the provided non-type template parameter depends on the deduced class template parameters.

// This cannot work, 'Enum' isn't deduced at point of declaration.
template <Enum E, class T, class Enum, size_t N>
constexpr T& get(enum_array<T, Enum, N>& a) noexcept {
    //return ...;
}

get<some_enum::val>(arr);

I understand c++17 has auto template parameters which would solve this issue, but unfortunately this code is c++14 only. Is there a way to deduce a non-type parameter type while still requiring it be provided by the user in c++14?


Solution

  • It seems this isn't possible in c++14. The best solution is to use a member function.