Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Template value before typename

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);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

Since C++17, you might do

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

Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading