I would like to do something like the following pattern:
T* const pT = findT();
// Do some work
T* const pT2 = new T( *pT );
// Mutate the object pT2 refers to
delete pT;
// At this point, I want the scope of pT to end.
// I do not want the scope of pT2 to end
I know I can end scope by ending a block, but it ends up like this:
T* pT2 = 0;
{
T* const pT = findT();
// Do some work
pT2 = new T( *pT );
// Mutate the object pT2 refers to
delete pT;
}
This causes pT2 to lose its const qualifier because I have to assign to it after it's declared.
I want my cake and I'd like to eat it too, I want clear constness and proper scoping!
Is there any way to end scope on a variable other than by ending a block? If not, are there any plans to extend the standard to support this?
You can use lambdas:
T* const pT = []() -> T* {
T* pT;
// Do whatever the hell you want with pT
return pT;
}();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With