I have a mathematical vector, whose dimensions I define with a template parameter int N
. The problem is that I add to many parameters at the constructor for up to N=9, g++ was OK with this, but the Intel compiler is complaining about this with a warning
warning #175: subscript out of range
Yes, the subscript is out of range as it appears, but no, it's not gonna cause a problem, because it's written like this:
template <int N, typename T>
Vector<N,T>::Vector(T val0, T val1, T val2, T val3, T val4, T val5, T val6, T val7, T val8, T val9)
{
for(int i = 0; i < N; i++)
{
_values[i] = T();
}
_values[0] = val0;
if(N > 1)
_values[1] = val1;
if(N > 2)
_values[2] = val2;
if(N > 3)
_values[3] = val3;
if(N > 4)
_values[4] = val4;
if(N > 5)
_values[5] = val5;
if(N > 6)
_values[6] = val6;
if(N > 7)
_values[7] = val7;
if(N > 8)
_values[8] = val8;
if(N > 9)
_values[9] = val9;
}
where N is the dimensionality, like I mentioned, and T is the data type. So the Intel compiler isn't smart enough to realize that this won't cause any problem since I don't access the element unless the the index is valid.
The question is: How can I tell the compiler with something like a macro/pre-compiler commands that it should not issue warning #175 for those lines? Notice that disabling warning #175 completely wouldn't be smart, though I could still do a real error somewhere else; I just want to disable it for this region of code.
Thanks for any efforts.
As it has been said, you can disable the warnings with compiler specific #pragma
s. The annoying thing of this solution is that it's not portable and some compilers emmit warnings when they find other compiler #pragma
s. So you end up just trade warnings.
You can fix this issue with yet another #pragma
or protecting the pragmas with
#ifdef SOME_MACRO_DEFINED_BY_COMPILER_A
#pragma SOME_PRAGMA_FOR_COMPILER_A
#endif
#ifdef SOME_MACRO_DEFINED_BY_COMPILER_B
#pragma SOME_PRAGMA_FOR_COMPILER_B
#endif
I would rather make sure that no compiler sees code that can raise this warning. I would do something (not tested) like:
template <int i>
typename std::enable_if<(i < N)>::type
set(const T& val) {
_values[i] = val;
}
template <int i>
typename std::enable_if<(i >= N)>::type
set(const T&) {
}
Vector(T val0, T val1, T val2, T val3, T val4, T val5, T val6, T val7, T val8, T val9) {
for(int i = 0; i < N; i++)
{
_values[i] = T();
}
_values[0] = val0;
set<1>(val1);
set<2>(val2);
set<3>(val3);
set<4>(val4);
set<5>(val5);
set<6>(val6);
set<7>(val7);
set<8>(val8);
set<9>(val9);
}