vendredi 22 octobre 2021

Disable copy assigment in CRTP-template

I'm having a CRTP template in which I use an object pool. Object are allocated using the generate() static method.

template <class tDerivedSignal, class tBridgeType, class tPayLoadType = void>
    class SignalT : public SignalSignatureT<tBridgeType>
   {
   ...
   static tDerivedSignal &generate(tPayLoadType &fPayLoad)
      {
      tDerivedSignal &rtnVal = Pool::reserve();

      // Copy pay load to signal instance
      rtnVal.mPayLoad = fPayLoad;

      // Return generated signal
      return(rtnVal);
      }
   ...
   }

Now, the thing is that I wan't to prevent a copy assignment like

DerivedSignal sig = DerivedSignal::generate()

The reason is that the copy sig may be have a local scope and become invalid/destructed which will generate a seg fault later on when the user tries to use it. Also, the allocated pool object will be lost and leak memory. I would like the copy assignment above to generate compile time error but a reference assignment such as

DerivedSignal &sig = DerivedSignal::generate()

shall be OK. I've tried to delete the assignment operator in the CRTP template with no luck:

template <class tDerivedSignal, class tBridgeType, class tPayLoadType = void>
    class SignalT : public SignalSignatureT<tBridgeType>
   {
   ...
   // Disable copy assignment operator to prevent a generated signal from being copied into
   // a new instance that is not under pool management
   tDerivedSignal operator=(const tDerivedSignal &) = delete;
   tDerivedSignal operator=(const tDerivedSignal) = delete;
   ...
   }

Anyone have any idea??

Aucun commentaire:

Enregistrer un commentaire