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??
CodePudding user response:
You want to delete the SignalT copy assignment (and most likely copy construction). By default, any class that inherits from this (as in your CRTP pattern) will have its default copy constructor/copy assignement operators also deleted, which will also inhibit the default move construct/move assignment operators.
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
void SignalT operator=(const SignalT&) = delete;
// You probably also want to delete copy construction
SignalT(SignalT const&) =delete;
...
}