Templates declarations in header files?

Posted on 1998-05-04
Last Modified: 2010-04-10
Is it legal to have only a template declaration in a header file if the template is defined somewhere in a regular file?  That is,

For example, can I just include the header file "SomeClass.h" into the "SomeProgram.cpp" as long as the file "SomeClass.cpp" is compiled and linked into the program (but not included into "SomeProgram.cpp").  

template <class T> class SomeClass
   int x;
   SomeClass(int i);  // Declared, not defined.
// Define constructor.
template <class T> SomeClass::SomeClass(int i);
  x = i;
#include "someclass.h"
  SomeClass SC(5);

A EE client was trying to do this in a program and I didn't think it was allowed,  I just want to make sure.  It caused linker errors, which seems to make sense as things compiled, but the necessary functions weren't there.
Question by:nietod

Expert Comment

ID: 1172021
Actually template class public functions must be declared inline.

Expert Comment

ID: 1172022
Yes, this is allowed according to the new C++ standard. It is called "separate template compilation". However, only a few compiler support it, and the ones I use are not among them :-(

See for a short explanation.
LVL 22

Author Comment

ID: 1172023
Sorry Galkin, but it looks like you are out of date.

Yonat, If you answer the question I'll give you the points.
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.


Expert Comment

ID: 1172024
I am not sure but I think that you have to use the extern keyword on the function bodies when they are not inline.
Danny Pav
LVL 22

Author Comment

ID: 1172025
Yes, that is correct.  Check the web site that yonat posted for details.

Accepted Solution

yonat earned 20 total points
ID: 1172026
LVL 10

Expert Comment

ID: 1172027
Gosh .. this C++ language thingy sure does get changed more than a baby on laxatives !!

It certainly keeps us poor experts on our toes .. and keeps those not-so-poor people that write compilers in jobs for a long time.


Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to split this in C++ 4 103
How to convert MFC APP to Win32 APP. 19 82
C++ :Change value from  DisableCMD registry 4 60
draw a Christmas tree by using a nested loop? 26 63
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question