Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Why can I separate the definition of my templates class from its declaration and put it inside a .cpp file?

0
Posted

Why can I separate the definition of my templates class from its declaration and put it inside a .cpp file?

0

If all you want to know is how to fix this situation, read the next two FAQs. But in order to understand why things are the way they are, first accept these facts: • A template is not a class or a function. A template is a “pattern” that the compiler uses to generate a family of classes or functions. • In order for the compiler to generate the code, it must see both the template definition (not just declaration) and the specific types/whatever used to “fill in” the template. For example, if you’re trying to use a Foo, the compiler must see both the Foo template and the fact that you’re trying to make a specific Foo. • Your compiler probably doesn’t remember the details of one .cpp file while it is compiling another .cpp file. It could, but most do not and if you are reading this FAQ, it almost definitely does not. BTW this is called the “separate compilation model.” Now based on those facts, here’s an example that shows why things are the way they are.

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123