I have a simple class:
template<size_t N, typename T>
class Int
{
bool valid(size_t index) { return index >= N; }
T t;
}
If I define an instance of this class as:
Int<0, Widget> zero;
I get a g++ warning:
warning: comparison is always true due to limited range of data type
I tried to do this, but I couldn't figure out how to partially specialize a function with a non-type template parameter. It looks like it might not be possible to disable this warning in g++. What is the开发者_开发技巧 proper way to either hide this warning, or to write this method such that it always returns true if N==0?
Thanks!
So, I've come up with the following solution:
template<size_t N>
bool GreaterThanOrEqual(size_t index)
{
return index >= N;
}
template<>
bool GreaterThanOrEqual<0l>(size_t index)
{
return true;
}
So now, the class looks like:
template<size_t N, typename T>
class Int
{
bool valid(size_t index) { return GreaterThanOrEqual<N>(index); }
T t;
}
Of course, I get an unused parameter warning, but there are ways around that....
Is this a reasonable solution?
You can specialize int for N = 0.
精彩评论