Elektrine lite

← Feed

@petrillic@hachyderm.io

Post #4118958

2026-07-26 19:01 UTC

@azonenberg@ioc.exchange I dunno, it feels like the "implementation" of a template class should go in the cpp file., not the hpp file? The idea of a class which has only an hpp, and no cpp seems... odd?

Replies (1)

  • @azonenberg@ioc.exchange 2026-07-26 19:05

    @petrillic@hachyderm.io 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.

    Open ##4118957