Skip to content

Disable implicit cast of smart pointer rvalues to raw pointers.

It is currently possible to introduce a subtle bug with methods returning vtkNew:

vtkNew<vtkObject> SomeFactoryMethod();

void bad()
{
  vtkObject *obj = SomeFactoryMethod(); // error, obj dangles
  obj->Print(std::cout);
}
void good()
{
  vtkNew<vtkObject> obj = SomeFactoryMethod(); // correct
  obj->Print(std::cout);
}

This patch deletes the rvalue-ref-qualified implicit cast operators from vtkNew and vtkSmartPointer, preventing these sorts of errors from compiling.

For cases where the cast is safe (such as passing a temporary to a vtkObject::SetXXX() method that does internal reference counting), using Get() explicitly bypasses the check:

vtkNew<vtkFieldData> fd;
fd->AddArray(vtkNew<vtkDoubleArray>{}); // error, cannot cast temp to ptr
fd->AddArray(vtkNew<vtkDoubleArray>{}.Get()); // ok

This way accidental casts will be caught, and intentional ones are allowed as long as intent is documented by the explicit function call.

Merge request reports