c – 错误“请求的对齐不是整数常量”
发布时间:2020-12-16 07:16:05 所属栏目:百科 来源:网络整理
导读:我在解决GCC问题时遇到了麻烦.我在GCC 4.8下体验它,但不是5.1.它看起来像 here和/或 here. 问题表面如下: template bool Bstruct S{ static const int ALIGN = 16; __attribute__((aligned(ALIGN))) int x;};int main(int argc,char* argv[]){ Strue s1; Sf
我在解决GCC问题时遇到了麻烦.我在GCC 4.8下体验它,但不是5.1.它看起来像
here和/或
here.
问题表面如下: template <bool B> struct S { static const int ALIGN = 16; __attribute__((aligned(ALIGN))) int x; }; int main(int argc,char* argv[]) { S<true> s1; S<false> s2; return 0; } 和: $g++ test.cxx -o test.exe test.cxx:9:41: error: requested alignment is not an integer constant __attribute__((aligned(ALIGN))) int x; 保持静态const也很重要,因为Clang不像GCC那样优化.而且C 03也是一项要求. 这是一个相关的问题,但它只是识别错误,并没有提供解决方法:Using constant from template base class. 我该怎么做才能解决这个问题? 这是问题编译器,但可能有其他人考虑其中一个问题仍然存在3年左右. $g++ --version g++ (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4 Copyright (C) 2013 Free Software Foundation,Inc. 实际使用案例涉及更多一点.如果机器提供SSE2或更高,则对齐为16.如果机器提供SSE4或更高,则然后对齐为32.否则,我们回退到自然对齐.所以它更接近: template <class W,bool B,unsigned int S> struct X { static const int ALIGN = (B ? 16 : sizeof(W)); __attribute__((aligned(ALIGN))) W x[S]; }; int main(int argc,char* argv[]) { X<int,true,10> x1; X<long,false,20> x2; return 0; } 解决方法
好吧,你说C 03支持是一个要求,我会回到(是的C-ish …)好的旧定义:
template <bool B> struct S { #define CONST_ALIGN 16 static const int ALIGN = CONST_ALIGN; // to allow using it later as S<B>.ALIGN __attribute__((aligned(CONST_ALIGN))) int x; // this uses a litteral int constant }; 当然,define不是结构的本地,并且可以在以下所有行中访问.但毕竟它并没有太大的伤害(*)并允许旧的编译器理解,而不重复一个神奇的时间(这里16). (*)它只能隐藏可能的拼写错误,如果在同一个文件中,您稍后使用声明附近: static const int CONST_ALIGNER = 12; ... int b = CONST_ALIGN; // TYPO should have been CONST_ALIGNER 这将导致难以找到错误 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |