0

Currently I have to work with legacy code and there is this struct:

typedef struct somesome {
  int val;
  std:atomic<int> index;
} somesome;

which is used inside this class:

class Blub {
protected:
  somesome blobb;
public:
  ...
  somesome getBlobb() const {return blobb;};
};

Because of the deleted copy constructor of atomic I can't return blobb, I believe... How can I return blobb the right way?

0xhph
  • 1
  • Do you need to return a copy? Can you return by reference? You can make your own copy constructor though, which just sets the value appropriately. – ChrisMM Mar 07 '21 at 13:16
  • 2
    Just implement your own or remove the `atomic`. In any case, watch out, because the author obviously didn't have a clue. – Ulrich Eckhardt Mar 07 '21 at 13:16
  • 2
    Why do you have an atomic member in there in the first place? It means the struct is used from multiple threads. Not a good idea to copy or move it! Maybe return a reference to it instead? – rustyx Mar 07 '21 at 13:21
  • @rustyx @ulrich-eckhardt @chrismm yeah I don't know what the exact idea of my previous colleague was it seems though he was doing some kind of threading.. I just tried to compile the code and got stuck. Anyway I tried to add my own constructors like so: `somesome() : val{0}, index{0} {} somesome(const somesome& data) : val{data.val}, index{data.index.load()} {}` – 0xhph Mar 07 '21 at 14:22
  • @rustyx copy is far better than a reference. mt read is np – Алексей Неудачин Mar 07 '21 at 15:00
  • @0xhph so you can't return it – Алексей Неудачин Mar 07 '21 at 15:59
  • @АлексейНеудачин what do you mean by mt read is no problem? – 0xhph Mar 07 '21 at 16:10
  • when several threads do read on same place in memory at the same time. So copy is better , but you really can't do it on this struct – Алексей Неудачин Mar 07 '21 at 18:13

0 Answers0