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...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help