Skip to content

classes are instantiated despite explicit instantiations #1334

@Raildex

Description

@Raildex

I have a header file vector4.h which contains the following:

#ifndef SRC_COMMON_VECTOR4_H
#define SRC_COMMON_VECTOR4_H
#include <glm/vec4.hpp>
#include <glm/gtc/quaternion.hpp>
namespace glm {
	extern template struct qua<float>;
	extern template struct vec<4, float, glm::defaultp>;
	extern template struct vec<4, long long, glm::defaultp>;
	extern template struct vec<4, long, glm::defaultp>;
	extern template struct vec<4, int, glm::defaultp>;
	extern template struct vec<4, unsigned long long, glm::defaultp>;
	extern template struct vec<4, unsigned long, glm::defaultp>;
	extern template struct vec<4, unsigned int, glm::defaultp>;
	extern template struct vec<4, short, glm::defaultp>;
	extern template struct vec<4, unsigned short, glm::defaultp>;
	extern template struct vec<4, signed char, glm::defaultp>;
	extern template struct vec<4, unsigned char, glm::defaultp>;
}
namespace raid {
	namespace math = ::glm;
	using quat = math::qua<float>;
	using float4 = math::vec<4, float, glm::defaultp>;
	using longlong4 = math::vec<4, long long, glm::defaultp>;
	using long4 = math::vec<4, long, glm::defaultp>;
	using int4 = math::vec<4, int, glm::defaultp>;
	using ulonglong4 = math::vec<4, unsigned long long, glm::defaultp>;
	using ulong4 = math::vec<4, unsigned long, glm::defaultp>;
	using uint4 = math::vec<4, unsigned int, glm::defaultp>;
	using short4 = math::vec<4, short, glm::defaultp>;
	using ushort4 = math::vec<4, unsigned short, glm::defaultp>;
	using byte4 = math::vec<4, signed char, glm::defaultp>;
	using ubyte4 = math::vec<4, unsigned char, glm::defaultp>;

} // namespace raid

#endif// SRC_COMMON_VECTOR4_H

I also have a corresponding vector4.cpp:

#include "common/vector4.h"
namespace glm {
	template struct qua<float>;
	template struct vec<4, float, glm::defaultp>;
	template struct vec<4, long long, glm::defaultp>;
	template struct vec<4, long, glm::defaultp>;
	template struct vec<4, int, glm::defaultp>;
	template struct vec<4, unsigned long long, glm::defaultp>;
	template struct vec<4, unsigned long, glm::defaultp>;
	template struct vec<4, unsigned int, glm::defaultp>;
	template struct vec<4, short, glm::defaultp>;
	template struct vec<4, unsigned short, glm::defaultp>;
	template struct vec<4, signed char, glm::defaultp>;
	template struct vec<4, unsigned char, glm::defaultp>;
}

However, the explicit instantiations do not seem to work (as I would expect them to).
Because whenever vector4.h gets included, glm automatically instantiates vec<4,T> in every combination.

I would expect my compiler to generate only one instantiation for the variations I specified in vector4.cpp.

main.cpp.json

Here is a trace of a file.

As you can see, the compiler (clang-cl in my case) instantiates all vector4's:
image

Is there a way to avoid this?

These are my glm compile defines:

GLM_FORCE_MESSAGES
GLM_FORCE_CXX98
GLM_FORCE_PURE
GLM_FORCE_LEFT_HANDED
GLM_FORCE_SWIZZLE
GLM_ENABLE_EXPERIMENTAL
GLM_FORCE_INLINE
GLM_FORCE_DEPTH_ZERO_TO_ONE

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions