在下面的示例中,clang 将各自的变量正确地放入“.aaa”和“.ggg”中。 GCC 适用于“.ggg”但不适用于“.aaa”(类模板的静态成员变量)。
template<int I>
struct s{
__attribute__((section(".aaa"))) static int a[100];
};
__attribute__((section(".ggg"))) int b[100];
template<int I>
__attribute__((section(".aaa"))) int s<I>::a[100];
这是 GCC 错误还是 clang 的自愿支持?
是否有好的解决方法(除了使 s::a 成为全局成员而不是静态成员之外)?
注意:我省略了编译器版本,因为 Godbolt 上所有版本的 GCC 基本上都做同样的事情 https://godbolt.org/g/E5s0mi
最佳答案
gcc的官方文档说
Use the section attribute with global variables and not local variables, as shown in the example.
本地联动 = 错误? 如果类本身是本地的,则静态成员变量是本地的。 我会通过模板声明说它是本地的。
关于c++ - 模板类的静态成员上的 __attribute__((section (".something"))) 是否允许?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42513904/