c++ - How to remove warnings about negative shift that will never occur beucase of correct flow branching? -
c++ - How to remove warnings about negative shift that will never occur beucase of correct flow branching? -
i interesting errors when compiling code using templates.
the problem compiler doesn't seem able compare numbers. gives warning whether 1 parameter bigger or smaller.
my minimal illustration looks this:
template< int shift > struct shifter { int value; template< int othershift > shifter< shift > &operator >>=( shifter< othershift > &rhs ) { if ( shift > othershift ) { this->value = rhs.value >> (shift - othershift); homecoming *this; } else { this->value = rhs.value << (othershift - shift ); homecoming *this; } } }; int main( void ) { shifter< 3u > = { 5 }; shifter< 2u > b = { 2 }; >>= b; b >>= a; }
when compiling next warnings:
in instantiation of 'shifter<shift>& shifter<shift>::operator>>=(shifter<othershift>&) [with int othershift = 2; int shift = 3 ]' warning: left shift negative number [enabled default] in instantiation of 'shifter<shift>& shifter<shift>::operator>>=(shifter<othershift>&) [with int othershift = 3; int shift = 2 ]' warning: right shift negative number [enabled default]
the warning message translated danish, might not verbatim.
the compilers mingw gcc 4.7.2 on windows iccarm iar
i need work using c++03 without boost, please no enable_if or "just utilize boost-this-and-that" answers.
btw unittests works fine i'd rather not have warnings when compiling code.
the problem is, compiler doesn't check if clause , can not know, whether perform negative shift.
you can avoid using absolute function:
#include <cstdlib> // ... template< int othershift > shifter< shift > &operator >>=( shifter< othershift > &rhs ) { if ( shift > othershift ) { this->value = rhs.value >> abs(shift - othershift); homecoming *this; } else { this->value = rhs.value << abs(othershift - shift ); homecoming *this; }
c++ templates
Comments
Post a Comment