仅标头模板(外部模板)的显式实例化声明 [英] Explicit instantiation declaration of header only template(extern template)

查看:81
本文介绍了仅标头模板(外部模板)的显式实例化声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加快GLM(OpenGL数学)的编译时间。 GLM大量使用C ++模板。

I am trying to speed up the compile time of the GLM(OpenGL Mathematics). GLM makes heavy usages of C++ templates.

这是我到目前为止尝试过的。

This is what I have tried so far.

math.h
#pragma once

#include <glm\glm.hpp>
extern template struct glm::tvec3<float, glm::highp>;







math.cpp
#include "math.h"
template struct glm::tvec3<float, glm::highp>;






然后我有三个文件正在使用 glm :: vec3 模板, glm :: vec3 glm :: tvec3< ; float,glm :: highp> 。三个文件 a,b,c 看起来几乎相同:


And then I have three files that are using the glm::vec3 template, glm::vec3 is a typedef of glm::tvec3<float, glm::highp>. The three files a,b,c looks almost the same:

a.cpp, b.cpp, c.cpp
#include "math.h"
glm::vec3 func() {
    glm::vec3 a = glm::vec3{1,1,1};
    glm::vec3 b = glm::vec3{1,1,1};
    return a + b;
}

我同时使用了显式实例化定义和显式实例化声明。因此,文件 a,b,c 不应引起隐式实例化。
但是编译时间与我不做的时间相同。

I am using both explicit instantiation definition and explicit instantiation declaration. So the files a,b,c should not cause implicit instantiation. But the compile time is the same as if I don´t do it.

推荐答案

您的math.h仍然导致用户包含< glm\glm.hpp>

这就是您要避免加快速度的事情。为了加快速度,请创建您自己的类,其实现(在math.cpp中)可能使用glm.hpp,但是该类的用户不需要自己包含glm.hpp。

Your math.h still causes users to include <glm\glm.hpp>
That's the thing you want to avoid to speed things up. To speed things up, make your own class whose implementation (inside math.cpp) might use glm.hpp, but the users of that class do not need to include glm.hpp themselves.

这是一个留给学生的示例,但您想要以下内容:

This is an example left to the student, but you want something like:

math.h

struct vec3 {double x1,x2,x3};

vec3 plus(const vec3& a,const vec3& b);

math.h
struct vec3{ double x1,x2,x3};
vec3 plus(const vec3& a, const vec3& b);

然后,当a.cpp包含math.h时,它将提供所需的功能,但不会使您的所有编译单元都包含glm.hpp。

Then when a.cpp includes math.h, it provides the functions you need, but does not make all your compilation units include glm.hpp.

这篇关于仅标头模板(外部模板)的显式实例化声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆