About C++ Templates

When you get to server.cpp in Tutorial 2 you'll see these lines at the bottom:

What's that all about?

Well, if you've been doing ACE for more than about 30 seconds you will have run into the joys and sorrows of C++ templates. They're really great things that prevent the need for complex #define'd macros, ensure type safety and do other really nifty things. One of the problems, however, is that not all compilers can figure out what templates you need.

Take the simple templated class:

Now suppose you write the following code fragment:

Some compilers will take care of you and automatically generate the equivalent classes:

On the other hand, some compilers will complain loudly about undefined symbols and all sorts of other things. When Clinton Carr compiled server.cpp of Tutorial 2 on his RedHat 5.1 (gcc) system, for instance, he was rewarded with these lovely errors:

Figuring out the correct manual instantiations is usually an iterative and tedious process. On Linux, I generally use a version of gcc that will do automatic instantiaion. "Normal" gcc with the Cygnus repo patches does that as does egcs. Lately (1/99) I've been using egcs 1.1.1 with pretty good results. On our Digital Unix 4.0b system the native compiler (CXX) has switches that will request it to also automatically instantiate templates as needed.

The tradeoffs?

If you choose to do manual instantiation then your code will work just about anywhere ACE will. For complex applications, it can take a number of hours to get things right.

If you choose to let the compiler do instantiations for you then it will perform the iterative process. That means that every compile will be longer than without manual instantiations.


Yes, you can do that. You can manually instantiate some templates and let the compiler get the rest. Some compilers will generate output that you can then use to figure out the correct templates. Gcc/egcs create .rpo files for each object. These files contain mangled names that the compiler uses to figure out what to instantiate. With c++filt and some patience, you can parse that stuff to figure out what the compiler is instantiating for you. Note that c++filt expects you to have a GNU-flavored C++ compiler available.

My best advice is to get a compiler that will handle the instantiations for you. When you have some free time on your hands, take a look at its intermediate files (if any) and start working on manual instantiation.

For some more hints, take a look at ACE-INSTALL

Thanks to Amos Shapira for catching a number of errors here.