简体   繁体   English

为volatile和非volatile实例重载类

[英]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.

相关问题 C++分配运算符在类的易失性和非易失性实例之间进行复制 - C++ assign operator to copy between volatile and non-volatile instances of a class 如何初始化具有非易失性结构的易失性结构? - How to initialise a volatile structure with a non-volatile structure? 避免易失性和非易失性成员函数的代码重复 - Avoid code duplication for volatile and non-volatile member function 定义用于易失性和非易失性对象的例程 - Defining routines for use with volatile and non-volatile objects 为什么可以相对于非易失性访问对这种易失性访问进行重新排序? - Why can this volatile access be reordered with respect to a non-volatile access? 易变超载? - volatile overloading? 非易失性C ++映射结构的备份方案 - Backup scheme for non-volatile C++ map constructs 通过易失性引用/指针访问声明的非易失性对象是否会在所述访问时赋予易失性规则? - Does accessing a declared non-volatile object through a volatile reference/pointer confer volatile rules upon said accesses? 非易失性数据的易失性指针 - Volatile Pointer to Non Volatile Data 我的应用程序创建的Windows CE /嵌入式C ++非易失性文件在重新启动时被删除 - Windows CE/Embedded C++ non-volatile files created by my app being deleted on reboot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM