jeudi 29 janvier 2015

Copying addresses of objects containing an atomic member?

Lets say I have a struct AtomicElement:



struct AtomicElement{
std::atomic<int32_t> a;
};


and I have another class, Object, which contains a reference to one of the above AtomicElement objects:



struct Object{
AtomicElement& ae;
};


Now elsewhere I have a vector of these AtomicElement objects and I would like to update Object::ae to point to different vector elements:



std::vector<AtomicElement> aeVector(AtomicElement());
Object obj;
.
.
//Obtain the address to the new element
AtomicElement& raw_ae = aeVector[i];
.
.
//Change to point to the new element
obj.ae = raw_ae; //Cannot get this part to compile.


What am I doing wrong?


My AtomicElement is only 32 bits, should I just be using by value?


I want to do this in the most efficient way possible, with as little copying as possible.


Aucun commentaire:

Enregistrer un commentaire