【问题标题】:G++ may_alias with Member Functions带有成员函数的 G++ may_alias
【发布时间】:2013-10-24 17:39:35
【问题描述】:

如何获得以下代码以在 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()

【问题讨论】:

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


    【解决方案1】:

    将属性直接放在struct 关键字之后:

    struct __attribute__((__may_alias__)) A
    {
      void foo();
    };
    
    void A::foo() {}
    
    int main() {return 0;}
    

    这适用于我的 g++4.7,而将它放在关闭 } 之后会产生与您得到的相同的错误。

    来自gcc documentation

    属性说明符列表可能作为structunionenum 说明符的一部分出现。它可以紧跟在structunionenum 关键字之后,也可以在右大括号之后。首选前一种语法。

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

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

    【讨论】:

    • 我已经完成了让成员内联并从中调用非内联免费函数的解决方法,但感谢您的回答。这确实为我解决了这个问题。我已经放弃了得到这个答案。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    相关资源
    最近更新 更多