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.
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

792 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