Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2006-11-29 22:06:52


Reusser, Edward wrote:
> This email was an eye popper for me because I have never had any luck
> declaring a template function in a header file and defining the
> implementation in a cpp file. I either get compilation/link errors,
> or
> very odd run-time problems. My understanding is that many compilers
> (of
> which mine, the MSVC 7.1 compiler) simply cannot handle this situation
> at all. I have always had to include the actual implementation of the
> template function inside the header file, which is a real pain.

What has worked well for me is the following:

a) declare the template in the header.
b) instantiate it in a *.cpp file

Works - but can lead to multiply defined symbols a link time.

So I use:

a) declare template in header.
b) create "implementation" *.ipp file
e) create *.cpp which
    i) includes the *.ipp file
    ii) explicitly instantiate the template. This is used to add
precompiled
    instantiations to the serialization libraries.

The serialiation library uses this for all compilers going back to msvc 6.0
and borland 5.51.

Robert Ramey


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net