Damus
Chris Petrilli · 6d
nostr:nprofile1qy2hwumn8ghj7un9d3shjtnyd968gmewwp6kyqpqqv5atqz9k9c54q8c28kra6sfata0wk7w7x5gkrnde8vmxe5gt00q8mmv7s I dunno, it feels like the "implementation" of a template class should go in the cpp f...
Andrew Zonenberg profile picture
@nprofile1q... think of templates more like macros, they're instructions on how to generate the class rather than the class itself.

The header has to be available when you instantiate the template because the specialization needs to be compiled when it's needed.

It is possible to explicitly instantiate specific overrides of a template in a source file (https://learn.microsoft.com/en-us/cpp/cpp/explicit-instantiation?view=msvc-170) but this is something you normally only do in unusual library use cases, or when a template is only ever going to have a small handful of instantiations.

You can also separate declarations and implementations into separate headers, sometimes needed for compile order purposes, but they still need to both be included in any translation unit that is going to create a new instantiation of the template.

Also, you can (and I often do) make either trivial struct-like classes with only data members, or ones that have tiny inline accessors only, entirely in headers with no source file even if no template is involved.