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.
Since C++17, you might do
template <auto L>
bool is_not_last(decltype(L) value) {
return value < L;
}
You can make your second argument in the template a default argument.
template<typename T, T last = T::_LAST>
bool is_not_last(T value){ return value < last; }
int main()
{
is_not_last(MyEnum::OTHER_VALUE); // No Error
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.