Home > Enterprise >  Template value before typename
Template value before typename

Time:12-02

I have following simplified example code where I attempt to figure out whether given value is the maximum value of enum of it's type.

enum class MyEnum : unsigned char {
    VALUE,
    OTHER_VALUE,
    _LAST
};

template<typename T, T _L>
bool is_not_last(T value) {
    return value < _L;
}

int main()
{
    is_not_last<MyEnum, MyEnum::_LAST>(MyEnum::OTHER_VALUE);

    return 0;
}

How can I format template so I can call is_not_last without specifying type first.

Desired outcome: is_not_last<MyEnum::_LAST>(MyEnum::OTHER_VALUE);

Following declarations didn't work:

template<T _L>
bool is_not_last(T value); // Doesn't have typename specified

template<typename T _L>
bool is_not_last(T value); // Invalid syntax

I feel like compiler should be able to deduce type from MyEnum::_LAST but I haven't been able to figure that out.

Thank you very much.

CodePudding user response:

Since C 17, you might do

template <auto L>
bool is_not_last(decltype(L) value) {
    return value < L;
}

Demo

  • Related