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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS