删除模板参数的g ++警告

Jar*_*edC 6 c++ templates g++

我有一个简单的课程:

template<size_t N, typename T>
class Int
{
    bool valid(size_t index) { return index >= N; }
    T t;
}
Run Code Online (Sandbox Code Playgroud)

如果我将此类的实例定义为:

Int<0, Widget> zero;
Run Code Online (Sandbox Code Playgroud)

我得到一个g ++警告:

warning: comparison is always true due to limited range of data type
Run Code Online (Sandbox Code Playgroud)

我试图这样做,但我无法弄清楚如何使用非类型模板参数部分特化一个函数.看起来可能无法在g ++中禁用此警告.隐藏此警告或写入此方法的正确方法是什么,如果N == 0,它总是返回true?

谢谢!

Jar*_*edC 7

所以,我想出了以下解决方案:

template<size_t N>
bool GreaterThanOrEqual(size_t index)
{
    return index >= N;
}

template<>
bool GreaterThanOrEqual<0l>(size_t index)
{
    return true;
}
Run Code Online (Sandbox Code Playgroud)

所以现在,这个班看起来像:

template<size_t N, typename T>
class Int
{
    bool valid(size_t index) { return GreaterThanOrEqual<N>(index); }
    T t;
}
Run Code Online (Sandbox Code Playgroud)

当然,我得到一个未使用的参数警告,但有办法解决....

这是合理的解决方案吗?