是否可以将非模板逻辑移至标头之外?

发布于 2025-01-17 08:42:58 字数 736 浏览 2 评论 0原文

我已将半边数据结构作为模板库实现。这样做的原因是,我不得不将此库从GLM移植到eigen,然后我不得不将其扩展到任意维度上的工作,但只有3D。

结果,我发誓从不只是在这里和那里乏味地更改类型,只是要有半边缘。

但是,尽管我的半边缘按预期运行并执行,但它是一个很大的标头,以至于我在任何包含它的文件上都花费了30%左右的编译时间。

令人沮丧的是,也许图书馆所做的事情的80%完全不可知几何数据。

一般概述将像

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 */}
};

在这种情况下一样vert,边,面,面孔都是非模板类别的类别,而该结构正在执行的80%涉及这三个类,而不是data

我想减少编译时间,因此理想情况下,我想将一些成员方法向下移动到CPP文件中,然后从标题中移出。

是否有一些语法要告诉C ++“对于任何模板,所有这些功能都是相同的,链接到CPP文件,让我将实现从标题中移出”?

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"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自我难过 2025-01-24 08:42:58

创建一个非模板基类,并将所有不依赖于模板参数的内容移至其中。其功能可以在.cpp文件中实现。

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.

海拔太高太耀眼 2025-01-24 08:42:58

“ PIMPL”技术是协助减少汇编时间的替代方法。

https://en.cppreference.com/w/cpp/langueage/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() {
}

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() {
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文