Microsoft VC++ implementation of class templates

Microsoft VC++ implementation of class templates

I am wondering how Visual C++ implements class templates. The code below is a simple stack class template that can take all the simple data types (char, int, float, double, etc.).

This small program will not compile. Apparently main.cpp cannot find the constructor, push and pop. The only way this runs is if I combine STACK.H and STACK.CPP into STACK.H and then build and run. Or if I combine all into one big MAIN.CPP.

Is there a way around this? Can I have these three files separate (which I would prefer) and still get the program to compile?

// STACK.H #include  #include   #ifndef STACK_H #define STACH_H  const int STACK_SIZE = 100;  template class stack {   private:          int count;          kind data[STACK_SIZE];   public:         //Initialize the stack          stack(void);          void push(const kind item);          kind pop(void); };   //STACK.CPP  #include “stack.h”  template stack::stack(void) {    count = 0; }  template void stack::push(const kind item) { data[count] = item; ++count; }  template kind stack::pop(void) { –count; return(data[count]); }  // MAIN.CPP  #include “stack.h”  void main(void) {   stack IntStack;  stack FloatStack;   IntStack.push(33);  FloatStack.push(3.14);   cout << IntStack.pop();  cout << FloatStack.pop();  }

The latest C++ draft paper specifies that all non-exported templates that are instantiated either implicitly of explicitly need to be declared and defined before the instantiation. This means that you must supply the implementation of the template before it is used.

In the past, various compilers have given options to solve this problem byproviding special ways to implement templates in one module andexport them to the other modules. The VC++ compiler, as far as I know, doesnot have any such facility.

Given this, what I always do is include my implementation file for templatesat the bottom of the header file. For example:

//stack.h#include template class stack {public:	stack(size_t size);	//…. othr stuff};#include “” // end stack.h// stack.cctemplate stack::stack(size_t size){	//. … implementation}// … other stuff// end main.cpp#include “stack.h”int main (){	stack i;	// … }// end main.cpp
Note that I call my template implementation file a .cc file so I can differentiate it from my other source files. Also, the .cc files are notused in any of the make files except as dependencies.

In the future, when more compilers are ANSI compliant, you will be able to export your class templates and take advantage of separate compilationfor templates.

See also  5 Tips for Working With an Onsite Interpreter

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist