Place the attribute directly after the struct
keyword:
struct __attribute__((__may_alias__)) A
{
void foo();
};
void A::foo() {}
int main() {return 0;}
This works for my g++4.7, while putting it after the closing }
yields the same error as you got.
From the gcc documentation:
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.
(The rest of the paragraph might reveal what's the underlying problem, and why it works when putting the attribute before the member-specification.)
Found this question by chance as you received the [tumbleweed] badge for it ;)