Compiler warning regarding template

1 #include <cstdlib> 2 #include <iostream> 3 4 enum { SIZE_PER_CHUNK = ((1<<16) / sizeof(unsigned)) }; 5 6 class TrueType {} ; 7 class FalseType {} ; 8 9 template< std::size_t V, bool B = (((V) & ((V) - 1)) == 0) > class IsPowerOfTwo_; 10 template< std::size_t V > class IsPowerOfTwo_< V, true > : public TrueType {}; 11 template< std::size_t V > class IsPowerOfTwo_< V, false > : public FalseType {}; 12 13 typedef IsPowerOfTwo_< SIZE_PER_CHUNK > IsPowerOfTwo; 14 15 16 int main() { 17 IsPowerOfTwo p2; 18 19 std::cout << "Hello World!" << std::endl; 20 return 0; 21 } 

The following code gives a warning about the compiler (gcc 4.6.2, / project / dfttools / compile / lnx-x86 / gcc-4.6.2):

warning: suggest parentheses around '-' in operand '&' [-Wparentheses]

The warning points to line 13, but this is probably due to the expression on line 9.

Any solution?

+4
source share
1 answer

Looks like a compiler error. It looks like the compiler removes unnecessary parentheses from the default value of B , and then triggers this warning.

Workaround:

 template<size_t V> struct IsPowerOfTwo_Helper { enum { value = V & (V - 1) }; }; template< std::size_t V, bool B = IsPowerOfTwo_Helper<V>::value == 0 > class IsPowerOfTwo_; 
+1
source

Source: https://habr.com/ru/post/1397011/


All Articles