Boost logo

Boost-Build :

Subject: Re: [Boost-build] vacpp and templates
From: VoidEx (VoidEx_at_[hidden])
Date: 2008-10-14 09:13:39


Templates must be defined in the same translation unit, where it used,
because linker can't generate infinite instantiations for any possible
template parameters.
That means that you must include it in any source.
Other way is to use explicit template instantiation, but you will able to
use only these explicitly instantiated templates. For example:

1.h (simple declaration, skipped)
1.cpp

template <typename type>
void foo() { ... }
template void foo<int>();
template void foo<float>();

2.cpp (includes 1.h)

void bar()
{
  foo(5); // ok, exists in 1.obj
  foo(1.f); // ok
  foo(43.); // unresolved external, because there's no function in 1.obj
}

For more information look 14.7.2 (Explicit instantiation) in standard.

Other way is to write templates in headers (or "inline" files) and include
them, that used in most cases.

>I am trying to compile a set of sources with templates in vacpp. Doing
>this, xlC tells me undefined references when is trying to link all the
>object files. These undefined references corresponds to template classes
>defined in header files.

>Any one had some experience on this? any idea?


Boost-Build list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk