C++ 关于模板的编译器警告 1#包括 2#包括 3. 4枚举{SIZE_PER_CHUNK=((1类ispowerroftow2,true>:public TrueType{}; 11模板类IsPowerOfTwo:public FalseType{}; 12 13 typedef IsPowerOfTwo_uIsPowerOfTwo; 14 15 16 int main(){ 17.两个p2; 18 19 std::cout
在我看来像是一个编译器错误。这就好像编译器从默认的C++ 关于模板的编译器警告 1#包括 2#包括 3. 4枚举{SIZE_PER_CHUNK=((1类ispowerroftow2,true>:public TrueType{}; 11模板类IsPowerOfTwo:public FalseType{}; 12 13 typedef IsPowerOfTwo_uIsPowerOfTwo; 14 15 16 int main(){ 17.两个p2; 18 19 std::cout,c++,templates,bit-manipulation,C++,Templates,Bit Manipulation,在我看来像是一个编译器错误。这就好像编译器从默认的B值中删除了不需要的括号,然后引发了警告本身 解决办法: 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
B
值中删除了不需要的括号,然后引发了警告本身
解决办法:
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 }
模板结构IsPowerOfTwo\u Helper
{
枚举{value=V&(V-1)};
};
模板类IsPowerOfTwo;
gcc有时会指向temlpate实例化的行,而不是定义所在的行。只需将括号放在那里,依靠运算符优先级规则通常不是一个好主意。括号太多了!请不要在您发布的代码中添加行号-它会复制和粘贴,以便在我的编译器unne中尝试您的代码非常难。(您可以在代码中使用注释来指示仍然相关的特定行)有人可以提出建议吗
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_;