[英]Overloading class for both volatile and non-volatile instances
I am writing a class that needs to support both volatile and non-volatile instances (volatile instances use atomic operations, non-volatile instances use regular operations), and am wondering if I am going about it in the correct way. 我正在编写一个需要同时支持volatile和非volatile实例的类(volatile实例使用原子操作,非易失实例使用常规操作),我想知道我是否以正确的方式处理它。 Here's a snippit of the class declaration so far: 到目前为止,这是一个类声明的嗤之以鼻:
class Yield {
public:
Yield();
Yield(Yield const &other);
Yield(Yield const volatile &other);
Yield &operator=(Yield const &other);
Yield &operator=(Yield const volatile &other);
Yield &operator+=(Yield const &other);
Yield &operator+=(Yield const volatile &other);
Yield volatile &operator+=(Yield const &other) volatile;
Yield volatile &operator+=(Yield const volatile &other) volatile;
// Other operators snipped...
};
Question 1: When compiling with MSVC, I get the following warning: 问题1:在使用MSVC进行编译时,我收到以下警告:
warning C4521: 'util::Yield' : multiple copy constructors specified
Does this warning portend any problems in using this class? 这个警告是否预示着使用这个类的任何问题? Or can it be safely ignored? 或者可以安全地忽略它?
Question 2: As it stands, all operators are overloaded for both a volatile and non-volatile other
argument. 问题2:就目前而言,所有运算符都因易失性和非易失性other
参数而过载。 I assume this is necessary in order to avoid slower volatile accesses for non-volatile instances? 我认为这是必要的,以避免非易失性实例的较慢的易失性访问? Is there an alternative to allow each method to be coded only twice (volatile lhs and non-volatile lhs) rather than 4 times (volatile and non-volatile lhs, each with volatile and non-volatile rhs)? 是否有一种替代方法允许每种方法只编码两次(易失性lhs和非易失性lhs)而不是4次(易失性和非易失性lhs,每种都有易失性和非易失性rhs)?
I hope putting these questions together is ok, otherwise please leave a comment and I can split them. 我希望将这些问题放在一起是好的,否则请发表评论,我可以将它们分开。 Thanks! 谢谢!
The class has multiple copy constructors of a single type. 该类具有单个类型的多个副本构造函数。 This warning is informational; 这个警告是信息性的; the constructors are callable in your program. 构造函数可以在程序中调用。
From the msdn website: Compiler Warning (level 3) C4521 来自msdn网站: 编译器警告(级别3)C4521
Volatile does not do what you think it does . 挥发性并不像你想象的那样 。
Even with VC++'s special, non-standard volatile
behavior, it results in slower code than writing it properly. 即使使用VC ++特殊的非标准volatile
性行为,也会导致代码比正确编写代码慢。 Use std::atomic
, or if that's not available then you've probably got platform-specific barrier, fence, and atomic intrinsics. 使用std::atomic
,或者如果那不可用,那么你可能已经获得了特定于平台的屏障,栅栏和原子内在函数。 VC++ has _ReadWriteBarrier
and _Interlocked
functions to help you. VC ++有_ReadWriteBarrier
和_Interlocked
函数来帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.