【发布时间】:2011-06-19 17:50:40
【问题描述】:
当前草案的以下摘录说明了我的意思:
namespace std {
typedef struct atomic_bool {
bool is_lock_free() const volatile;
bool is_lock_free() const;
void store(bool, memory_order = memory_order_seq_cst) volatile;
void store(bool, memory_order = memory_order_seq_cst);
bool load(memory_order = memory_order_seq_cst) const volatile;
bool load(memory_order = memory_order_seq_cst) const;
operator bool() const volatile;
operator bool() const;
bool exchange(bool, memory_order = memory_order_seq_cst) volatile;
bool exchange(bool, memory_order = memory_order_seq_cst);
bool compare_exchange_weak(bool&, bool, memory_order, memory_order) volatile;
bool compare_exchange_weak(bool&, bool, memory_order, memory_order);
bool compare_exchange_strong(bool&, bool, memory_order, memory_order) volatile;
bool compare_exchange_strong(bool&, bool, memory_order, memory_order);
bool compare_exchange_weak(bool&, bool, memory_order = memory_order_seq_cst) volatile;
bool compare_exchange_weak(bool&, bool, memory_order = memory_order_seq_cst);
bool compare_exchange_strong(bool&, bool, memory_order = memory_order_seq_cst) volatile;
bool compare_exchange_strong(bool&, bool, memory_order = memory_order_seq_cst);
atomic_bool() = default;
constexpr atomic_bool(bool);
atomic_bool(const atomic_bool&) = delete;
atomic_bool& operator=(const atomic_bool&) = delete;
atomic_bool& operator=(const atomic_bool&) volatile = delete;
bool operator=(bool) volatile;
} atomic_bool;
}
易失性是可传递的。因此,您不能从易失性对象调用非易失性成员函数。另一方面,允许从非易失性对象调用易失性成员函数。
那么,原子类中的 volatile 和 non-volatile 成员函数在实现上有什么区别吗?换句话说,非易失性重载有必要吗?
【问题讨论】:
-
@GMan:因为否则无法对易失性数据调用函数。 ;)
-
@jalf:哈,是的,但是由于类型本身进行的操作是原子的(因此是可观察的),我们为什么要创建
volatile atomic<>?我想我错过了一些重要的东西。 -
@GMan:但为什么不允许呢?假设您有一个
atomic<>作为另一个结构的成员,并且创建了一个volatile实例?然后它的所有成员也将隐含为volatile,并且没有volatile重载,您闪亮的新atomic类将毫无用处。 :) -
@GMan:完全正确。如果对象 表现 就好像它是 volatile 一样,那么如果我添加
volatile限定符,它也应该可以工作,不是吗?您是否还会争辩说“如果该类在语义上仍然是恒定的,那么标记类const的成员是没有意义的”?没有区别,除了允许类在相关限定符存在的上下文中继续工作 -
@jalf:“如果对象表现得好像它是 volatile 的,那么如果我添加 volatile 限定符,它也应该工作,不是吗”也许,但我的问题是为什么我们甚至需要 支持,如果添加 they 关键字无论如何都没有区别。您的
const示例非常适合说明为什么我们需要在成员函数上使用const,但是如果我们有一个本质上是 const 的类型呢? (例如,std::integral_constant。)创建const的实例有什么好处?
标签: c++ c++11 volatile member-functions qualifiers