【问题标题】:Is load() called implicitly when using a method from an std::atomic?使用 std::atomic 中的方法时是否隐式调用 load()?
【发布时间】:2017-10-20 05:48:22
【问题描述】:

我注意到,至少在表面上,最后几行代码似乎是等价的:

std::atomic<int*> a;
a.store(new int{11});
std::cout << *a.load() << "\n";
std::cout << *a << "\n";

似乎可以通过直接调用解引用运算符或加载指针然后调用运算符来访问存储在指针上的原子包装器中的值。

这两者有区别吗?解引用运算符的使用是否隐式调用 load() ?

【问题讨论】:

    标签: c++ pointers c++14 atomic c++17


    【解决方案1】:

    a 调用了operator int*(),然后将间接运算符应用于指针。转换函数和调用load是一样的,所以几乎是一样的。

    唯一的区别是类型是用户定义的,因为它会消耗您的一个用户定义的隐式转换。

    【讨论】:

    • 等等...你的意思是,a 调用了运算符 int*() 然后调用了间接运算符...这不是完全相同的东西,是你是说这行代码被解释为 *a (似乎不是这样)?我假设 load 是隐式调用的,因为 a 没有运算符 "" 但 a.load() 有......但现在我更困惑
    • @George operator*() 是间接运算符,operator int*() 是转换运算符。 operator int*() 调用load() 并返回int*,其中使用了*
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 2016-01-02
    • 2015-05-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 2012-08-04
    相关资源
    最近更新 更多