c++ - 具有成员函数的 G++ may_alias

标签 c++ g++-4.7 gcc-extensions

如何让下面的代码在 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,而将其放在结束 之后会产生与您遇到的错误相同的错误。

来自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.

(本段的其余部分可能会揭示潜在的问题是什么,以及将属性放在成员规范之前的原因。)

在收到 [tumbleweed] 徽章时偶然发现了这个问题 ;)

关于c++ - 具有成员函数的 G++ may_alias,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19572798/

相关文章:

c++ - 并行化 SVD 计算 c++

c++ - 可以在调用构造函数之前完成赋值吗?

C++ 友元运算符重载嵌套枚举的模板参数

c++ - 错误 "' fdopen' was not declared"found with g++ 4 that compiled with g++3

c++ - 如何将 Arduino 与 Microsoft Visual Studio 一起使用(由于 #include_next 预编译器问题)

c - C代码中switch-case中的 "..."是什么

c++ - 字节顺序标记真的是一个有效的标识符吗?

c++ - 在 Windows 上默认启用 GCC 编译器 C++11 标志

c++ - C++ 的通用线程 C 包装器函数

c++ - 函数返回类型说明符 c++type 中的变量声明