3 ms·
You can declare a template in a header file, and only provide its definition (and hence expansion) in a source file. See for example Firefox doing this for its
by jcranmer 2y ago
You can declare a template in a header file, and only provide its definition (and hence expansion) in a source file. See for example Firefox doing this for its string implementation here: https://searchfox.org/mozilla-central/source/xpcom/string/nsTSubstring.h#289 https://searchfox.org/mozilla-central/source/xpcom/string/ns... (extern template declarations are at the end of the header file, and the actual template definitions are in https://searchfox.org/mozilla-central/source/xpcom/string/nsTSubstring.cpp https://searchfox.org/mozilla-central/source/xpcom/string/ns...).
Which is to say, "extern template" is a thing that exists, that works, and can be used to do what you want to do in many cases.
The "export template" feature was removed from the language because only one implementer (EDG) managed to implement them, and in the process discovered that a) this one feature was responsible for all of their schedule misses, b) the feature was far too annoying to actually implement, and c) when actually implemented, it didn't actually solve any of the problems. In short, when they were asked for advice on implementing export, all the engineers unanimously replied: "don't". (See https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1426.pdf https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n14... for more details).