zennehoy
zennehoy

Reputation: 6856

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. 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...
};

I hope putting these questions together is ok, otherwise please leave a comment and I can split them. Thanks!

Upvotes: 3

Views: 213

Answers (2)

Sujay Anand
Sujay Anand

Reputation: 31

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

Upvotes: 3

Cory Nelson
Cory Nelson

Reputation: 30011

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. Use std::atomic, or if that's not available then you've probably got platform-specific barrier, fence, and atomic intrinsics. VC++ has _ReadWriteBarrier and _Interlocked functions to help you.

Upvotes: 2

Related Questions