Why export templates would be useful in C++ (2010)
warp.povusers.org·17h·
Flag this post

Export templates

The C++ standard defines the concept of “export templates”. These are template classes and functions which are only declared wherever they are used (ie. usually in a header file), while implemented only once in some compilation unit.

In other words, you could have a header file like:

// the_header_file.hh
export template<typename T>
void foo(const T& value);

And then you could use it somewhere like:

#include "the_header_file.hh"

int main()
{
foo(123);
foo("456");
}

Note how the calling code inside main() doesn’t see how the foo() template function has been implemented, like it’s usual with regular templates. It only sees a declaration of it.

The implementation of foo() can then be put in some source file to be compiled separately…

Similar Posts

Loading similar posts...