我正在编写一个类,它需要同时支持易失性实例和非易失性实例(易失性实例使用原子操作,非易失性实例使用常规操作),并且想知道我是否以正确的方式处理它。到目前为止,下面是类声明的一个片段:
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...
};warning C4521: 'util::Yield' : multiple copy constructors specified
这个警告预示着在使用这个类时会出现什么问题吗?或者它可以安全地被忽视吗?other参数,所有操作符都会被重载。我认为这是必要的,以避免对非易失性实例进行较慢的易失性访问?是否可以允许每种方法只编码两次(易失性lhs和非易失性lhs)而不是4次(易失性和非易失性lhs,每种方法都有易失性和非易失性rhs)?我希望把这些问题放在一起是可以的,否则请留下评论,我可以把它们分开。谢谢!
发布于 2012-12-14 19:54:49
即使使用VC++的特殊的、非标准的volatile行为,它也会导致代码比正确编写代码要慢。使用std::atomic,或者如果这是不可用的,那么您可能有特定于平台的屏障、栅栏和原子本质。VC++有_ReadWriteBarrier和_Interlocked功能来帮助您。
发布于 2012-12-14 19:48:04
该类具有单个类型的多个副本构造函数。此警告是信息性的;可在程序中调用构造函数。
来自msdn网站:编译器警告(3级) C4521
https://stackoverflow.com/questions/13867480
复制相似问题