小编ger*_*rdi的帖子

当你是朋友时,为什么GCC不允许继承私有嵌套类?

同样的问题:为什么GCC允许从私有嵌套类继承?对于非模板类,如果它是朋友,则允许从私有嵌套类继承,但不允许从模板类继承.这是一个错误吗?

template<class Base>
class InheritFromBaseMember : public Base::MemberPrivate // error
{
    using PrivateMember = typename Base::MemberPrivate; // works fine
};

class MyBase{
    friend class InheritFromBaseMember<MyBase>;

    // another try to declare it friend
    template<class T>
    friend class InheritFromBaseMember;

    friend class AnotherClass;

    class MemberPrivate{};
};

class AnotherClass : public MyBase::MemberPrivate{}; // works fine

int main() {
    InheritFromBaseMember<MyBase>{};
}
Run Code Online (Sandbox Code Playgroud)

来自g ++ 5.3.0的错误消息:

error: 'class MyBase::MemberPrivate' is private
     class MemberPrivate{};
           ^
error: within this context
 class InheritFromBaseMember : public Base::MemberPrivate // error
       ^
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates nested friend

9
推荐指数
1
解决办法
156
查看次数

是对基本类型的非原子变量的并发写入和读取而不使用它的未定义行为吗?

在无锁的queue.pop()中,我在与循环内的原子获取同步后读取了一个trivialy_copyable变量(整型)。\n最小化的伪代码:

\n
//somewhere else writePosition.store(...,release)\n\nbool pop(size_t & returnValue){\nwritePosition = writePosition.load(aquire)\noldReadPosition = readPosition.load(relaxed)\nsize_t value{};\ndo{\n  value = data[oldReadPosition]\n  newReadPosition = oldReadPosition+1\n}while(readPosition.compare_exchange(oldReadPosition, newReadPosition, relaxed)\n// here we are owner of the value\nreturnValue = value;\nreturn true;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

data[oldReadPosition]仅当该值之前从另一个线程读取时,才能更改内存。

\n

读写位置都是 ABA 安全的。\n通过简单的复制,value = data[oldReadPosition]内存data[oldReadPosition]不会被改变。

\n

但是写入线程queue.push(...)可以在读取时更改data[oldReadPosition],前提是另一个线程已经读取了 oldPosition 并更改了 readPosition。

\n

如果您使用该值,这将是一个竞争条件,但是当我们保持value不变时,它是否也是一个竞争条件,从而导致未定义的行为?标准不够具体或者我不\xc2\xb4不理解它。\nimo,这应该是可能的,因为它没有效果。\n我会很高兴得到一个合格的答案以获得更深入的见解

\n

多谢

\n

c++ concurrency lock-free race-condition stdatomic

4
推荐指数
1
解决办法
297
查看次数