I was just in class, learning about STL and class templates. My teacher said you cannot split the class declaration and the function definitions into the normal header/cpp file like you can with non template classes.

Is this true, and if so why? I assume it would have somthing to do with the linking process because you can choose any datatype to work with in templates?

Thanks

Dani AI

Generated

Short answer: yes — the compiler must see a template's definitions where a particular specialization is instantiated. Templates are instantiated at compile time in each translation unit that uses them, so merely putting declarations in a header and definitions in a .cpp that is not included will usually cause missing-instantiation or linker errors.

Practical patterns used in real projects:

  • Header-only: put both declaration and definition in the header (common in libraries like the STL).
  • Header + implementation file included at the bottom of the header (keeps code visually separated but still visible to the compiler). Example pattern:
// widget.h
template<typename T>
class Widget { /* ... */ };

#include "widget.tpp"   // contains Widget<T>:: member definitions
  • Precompile a fixed set of specializations: as suggested, produce explicit instantiations in one .cpp so that only that object file contains the generated code. Use extern template declarations in headers to prevent other TUs from implicitly instantiating the same types, which helps compile times and avoids duplicate-code growth.

A few practical tips and caveats:

  • If you see linker messages like undefined references for MyTemplate<SomeType>::member, the TU that needs that specialization does not have the definition visible.
  • If you get multiple-definition problems, ensure only one TU provides the instantiation (use explicit instantiation + extern template).
  • Do not depend on the historical export facility — it is rarely implemented by mainstream compilers and not a portable solution (as and noted).

For authoritative details on instantiation rules and extern template, see the template-instantiation documentation on cppreference: . For modern alternatives that ease separation, read about C++20 modules: modules.

Recommended Answers

All 4 Replies

I was just in class, learning about STL and class templates. My teacher said you cannot split the class declaration and the function definitions into the normal header/cpp file like you can with non template classes.

Is this true, and if so why? I assume it would have somthing to do with the linking process because you can choose any datatype to work with in templates?

Thanks

Yes for now its true. Although if your compiler supports the
keyword export, then you can separate the definition and the header file. Did you ask the teacher about this ? She
would have probably gave you a through answer.

For now? Are they fixing this currently?

Actually, it is only partially true. For many template classes you know that you are only going to have a few well known types to create. In that case you can use explicit template initialization. e.g.

//file  test.h
template<typename T>
class test
{
   T x;
public: 
   void addStuff(const T&);
   // ... 
};
// FILE: test.cpp
template<typename T>
void test<T>::addStuff(const T& A)
{
    x+=A;
} 
// THIS IS THE BIT TO create instances
template class test<double>;
template class test<std::complex<double> >;
template class test<int>;

So as you can see you can separate BUT you have to know what you are going to need. It is reasonably easy to do if you are the end user of the templates AND it speeds up compiles dramatically.

I have some (horrible) perl scripts that strips the error file from gcc to add the correct instances, but it isn't that difficult to do by hand or write something a million times better than my scripts.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.