如何让下面的代码在 g++ 4.7 上编译?如果我将 foo
的主体内联,它会编译,但我不希望它内联(因为真正的代码要复杂得多)。
struct A
{
void foo();
} __attribute__((__may_alias__));
void A::foo() {}
int main() {return 0;}
错误:
/tmp/test.cpp:6:6: error: prototype for ‘void A::foo()’ does not match any in class ‘A’
/tmp/test.cpp:3:8: error: candidate is: void A::foo()
最佳答案
将属性直接放在 struct
关键字之后:
struct __attribute__((__may_alias__)) A
{
void foo();
};
void A::foo() {}
int main() {return 0;}
这适用于我的 g++4.7,而将其放在结束 之后会产生与您遇到的错误相同的错误。
An attribute specifier list may appear as part of a
struct
,union
orenum
specifier. It may go either immediately after thestruct
,union
orenum
keyword, or after the closing brace. The former syntax is preferred.
(本段的其余部分可能会揭示潜在的问题是什么,以及将属性放在成员规范之前的原因。)
在收到 [tumbleweed] 徽章时偶然发现了这个问题 ;)
关于c++ - 具有成员函数的 G++ may_alias,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19572798/