c – 有关模板的编译器警告
发布时间:2020-12-16 06:58:55 所属栏目:百科 来源:网络整理
导读:1 #include cstdlib 2 #include iostream 3 4 enum { SIZE_PER_CHUNK = ((116) / 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:
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 } 以下代码给出了编译器警告(gcc 4.6.2,/ project / dfttools / compile / lnx-x86 / gcc-4.6.2):
警告指向第13行,但它可能与第9行中的表达式有关. 有解决方案吗 解决方法
看起来像编译器错误给我.就好像编译器从默认B值中删除不需要的括号,然后它会引发警告本身.
解决方法: 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_; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |