Explicit declaration of the header-only template instance (extern template)

I am trying to speed up GLM compilation time (OpenGL math). GLM makes heavy use 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>; 

And then I have three files that use the glm::vec3 , glm::vec3 is the typedef from glm::tvec3<float, glm::highp> . The three files a,b,c look 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; } 

I use both an explicit definition of instantiation and a declaration of explicit instantiation. Therefore, files a,b,c should not cause implicit instantiation. But the compilation time is the same as if I did not.

+8
c ++ c ++ 11 templates extern
source share
1 answer

Your math.h still forces users to include <glm \ glm.hpp>
This is what you want to avoid in order to speed up the process. To speed things up, create your own class, the implementation of which (inside math.cpp) can use glm.hpp, but users of this class do not need to include glm.hpp.

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);

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

0
source share

All Articles