【问题标题】:Using pointers-to-members and address spaces in Clang在 Clang 中使用指向成员的指针和地址空间
【发布时间】:2015-12-24 14:13:56
【问题描述】:

我正在处理具有多个地址空间的目标上的 C++ 代码。我想使用的代码有指向成员的指针:

struct foo {
    int bar;
    int baz;
};

typedef int foo::*foo_member;
foo_member m = &foo::bar;

#define AS1 __attribute__((address_space(1)))

int main()
{
    foo AS1* f = /* ... */;
    f->*m = 4;
}

到目前为止一切顺利。但是,当我尝试获取指向实际成员的指针时,事情变得很有趣:

int main()
{
    foo AS1* f = /* ... */;
    int AS1* x = &(f->*m);
}

Clang 3.7.0 (tags/RELEASE_370/final) 抱怨:

test.cpp:14:11: error: cannot initialize a variable of type
      '__attribute__((address_space(1))) int *' with an rvalue of type 'int *'
        int AS1* x = &(f->*m);
                 ^   ~~~~~~~~

似乎地址操作符(或成员解引用?)删除了地址空间限定符。

如何在确保地址空间不被丢弃的同时使用指向具有地址空间的成员的指针?

【问题讨论】:

  • 你试过把属性放在指针类型里吗? using foo_member = int foo::* AS1;,还是using foo_member = int AS1 foo::*;
  • @KerrekSB,我也不清楚AS1 限定符应该放在哪里,但错误要么保持不变,要么抱怨您无法使用&foo::bar 对其进行初始化。
  • 只有&(f->bar) 有效吗?也许它与指向成员的指针无关?
  • @KerrekSB,是的,它返回地址空间 1 中的指针。
  • 在我看来像一个编译器错误。

标签: c++ clang


【解决方案1】:

这是一个编译器错误。一直是reported,已经提交了补丁。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    相关资源
    最近更新 更多