mercredi 15 septembre 2021

Is it possible to use return value move semantics with a protected copy constructor in C++?

I am using the CGAL library and writing a function that I hope to use move semantics as described in this question:

PointTree preparePointTree(const vector<PlyPoint>& pointCloud) {
  PointTree tree;
  kd_tree::prepare<...>(pointCloud, tree);
  return tree;
}

and I'm calling it like:

PointTree tree = preparePointTree(pointCloud);

Unfortunately, it does not compile; GCC complains that:

return tree;  // <-- error: 'CGAL::Kd_tree<...>' is private within this context

with note:

private:
  // protected copy constructor
  Kd_tree(const Tree& tree)   // <-- note: declared private here

which is from this code.

So the issue seems to be that returning tree is illegal if the copy-constructor is unavailable, even though I don't want to copy, but to move.

I suppose that's because because move semantics depend on the call site (the call to preparePointTree()), and the function implementation by itself is treated as "has to compile even in the non-move case".

So, what now?

Is there something that I, or the CGAL library, can do to allow moving but not copying?

What's the reasoning behind this?

Aucun commentaire:

Enregistrer un commentaire