类型名之前的模板值

Adr*_*ček 4 c++ templates

我有以下简化的示例代码,我试图找出给定值是否是其类型的枚举的最大值。

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;
}
Run Code Online (Sandbox Code Playgroud)

如何格式化模板,以便我可以is_not_last在不先指定类型的情况下进行调用。

期望的结果: is_not_last<MyEnum::_LAST>(MyEnum::OTHER_VALUE);

以下声明不起作用:

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
Run Code Online (Sandbox Code Playgroud)

我觉得编译器应该能够从中推断出类型MyEnum::_LAST,但我还没能弄清楚。

非常感谢。

Jar*_*d42 8

从 C++17 开始,你可能会这样做

template <auto L>
bool is_not_last(decltype(L) value) {
    return value < L;
}
Run Code Online (Sandbox Code Playgroud)

演示