Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template value before typename

Tags:

c++

templates

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.

like image 981
Po1nt Avatar asked Jan 25 '23 04:01

Po1nt


1 Answers

Since C++17, you might do

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

Demo

like image 89
Jarod42 Avatar answered Jan 26 '23 18:01

Jarod42