同样的问题:为什么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) 在无锁的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}\nRun Code Online (Sandbox Code Playgroud)\ndata[oldReadPosition]仅当该值之前从另一个线程读取时,才能更改内存。
读写位置都是 ABA 安全的。\n通过简单的复制,value = data[oldReadPosition]内存data[oldReadPosition]不会被改变。
但是写入线程queue.push(...)可以在读取时更改data[oldReadPosition],前提是另一个线程已经读取了 oldPosition 并更改了 readPosition。
如果您使用该值,这将是一个竞争条件,但是当我们保持value不变时,它是否也是一个竞争条件,从而导致未定义的行为?标准不够具体或者我不\xc2\xb4不理解它。\nimo,这应该是可能的,因为它没有效果。\n我会很高兴得到一个合格的答案以获得更深入的见解
多谢
\nc++ ×2
concurrency ×1
friend ×1
inheritance ×1
lock-free ×1
nested ×1
stdatomic ×1
templates ×1