Home > Mobile >  Is it possible to move non-template logic outside of the header?
Is it possible to move non-template logic outside of the header?

Time:03-28

I have implemented a half edge data structure as a template library. The reason for this is, I have had to port this library from glm to eigen, and then I had to extend it to work on arbitrary dimensions, but just 3D.

As a result I vowed to never just tediously change types here and there just to have a half edge.

However, although my half edge runs and performs as expected, it's such a large header that it is taking 30% or so of my compile times on any file that includes it.

What is frustrating is, maybe 80% of what the library is doing is completely agnostic to what the geometric data is.

The general overview would be something like

template<typename Data>
class HalfMesh
{
vector<Data> data;
vector<HalfEdge> verts;
vector<HalfEdge> edges;
vector<Face> faces;

void SomeMethod(){/* uses only verts, edges, and faces */}
};

In this scenario verts, edges, faces are all non templated classes and 80% of what this structure is doing involves those 3 classes and not Data.

I want to reduce my compile times, so ideally I want to move some member methods down into a cpp file and out of the header.

Is there some syntax to tell c "all this functions are the same for any template, link to a cpp file and let me move the implementation out of the header"?

CodePudding user response:

Create a non-template base class, and move everything that doesn't depend on template parameters to it. Its functions can be implemented in a .cpp file.

CodePudding user response:

The technique 'pimpl' is an alternate method for assisting in the reduction of compilation time.

https://en.cppreference.com/w/cpp/language/pimpl

// ------------- half_mesh.h --------------------------
template <typename Data>
class HalfMesh {
 private:
  vector<Data> data;
  std::unique_ptr<HalfMeshImpl> impl;

 public:
  inline void SomeMethod() {
    impl->SomeMethod();
  }
};

// ------------- half_mesh_impl.h --------------------------
class HalfMeshImpl {
 private:
  vector<HalfEdge> verts;
  vector<HalfEdge> edges;
  vector<Face> faces;

 public:
  void SomeMethod();
};

// ------------- half_mesh_impl.cc --------------------------
void HalfMeshImpl::SomeMethod() {
}
  • Related