Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing g++ warning for template parameter

Tags:

c++

templates

g++

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!

like image 907
JaredC Avatar asked Jan 07 '11 22:01

JaredC


2 Answers

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?

like image 53
JaredC Avatar answered Sep 28 '22 08:09

JaredC


You can specialize int for N = 0.

like image 22
Puppy Avatar answered Sep 28 '22 07:09

Puppy