2

How can I get the below code to compile on g++ 4.7? It will compile if I place the body of foo inline, but I don't want it inline (because the real code is a lot more complicated).

struct A
{
  void foo();
} __attribute__((__may_alias__));

void A::foo() {}

int main() {return 0;}

Error:

/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()
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98

1 Answers1

1

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 or enum specifier. It may go either immediately after the struct, union or enum 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 ;)

dyp
  • 38,334
  • 13
  • 112
  • 177
  • I had done a workaround of having the member inline and call a non-inline free function from it, but thanks for the answer. This does indeed solve the issue for me. I had given up on getting an answer to this. :) – Dark Falcon Nov 21 '13 at 14:29