Re: Member function templates in libraries

From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 08/10/04


Date: Tue, 10 Aug 2004 09:47:11 -0400

Arne Petersen wrote:
> Hy,

Hy yourself.

>
> I've got a problem with member function templates compiled into
> libraries.
> I'm trying to get a library collection (coded for GNU gcc, where its
> compiled completly) being compiled on Visual Studio .NET. The problem is
> that for gcc the template functions (members of a class) are explicitly
> instantiated in a cpp file that includes the classes .h and .cpp file.

Let me understand, the template functions are instantiated in a cpp file
that includes (among other things) the cpp file, right?

> The VS compiler does not correctly bring together the function template
> declaration and its instanciation.

What's "incorrect" about it? Could you be a bit more verbose here?

> An other problem is that in VS it's not possible to declare just the
> generic function template in the header and define the specialization in
> the .cpp file. At least a declaration of the specialized template has to
> be written in the header.

It's not possible in any compiler that exists today that doesn't implement
"export" keyword (and I'm yet to see one that does).

> Now how can I get GNU version libraries be compiled under VS?

If you need a compiler-specific solution, you need to ask in a newsgroup
dedicated to that compiler. However, it is usually enough to instantiate
templates explicitly to place them into the library:

-------------------- some.h
template<class T> void foo(T t);
-------------------- some.cpp
#include <some.h>
template<class T> void foo(T t) {
     42;
}

template void foo(int);
template void foo(double);
template void foo(const char*);
#include <myspecialclass.h>
template void foo(MySpecialClass&);
------------------------------------------

All those declarations that begin with 'template' without the argument
specification after it are the explicit instantiations. When the compiler
sees an explicit instantiation, it generates code for that function. Next
time there is a call to that function, the compiler doesn't need to see
the body of the function, it should be resolved at link time.

The advantage of this is that you can put the implementation in a separate
C++ translation unit, thus hiding it from the user of the template. Also,
the compiler doesn't waste time compiling the template definition every
time it sees that it's used.

The disadvantage if it is that you have to be able to predict for which
template arguments your template is going to be used (and make that list
exhaustive). In the example above, if I ever try using foo(100L), then
I got a problem: unresolved external "void foo<long>(long);" because there
is no explicit instantiation for it in my library, and the compiler cannot
instantiate it at the time of use because I didn't provide the definition.

Victor



Relevant Pages

  • Re: name binding from one template to another
    ... compiler binds another referred template from one template in the ... The compiler can look up primary template itself in the first phase - ... I am wrong that compiler will do nothing to bind the referred ... compiler won't instantiate vector until it has to instantiate func. ...
    (microsoft.public.vc.language)
  • Re: Inconsistency in exported symbols between different compiler versions
    ... specialization of a class template. ... In this case, the compiler implicitly ... If two libraries both used the explicit dllexport to export the same ...
    (microsoft.public.vc.language)
  • Re: OOP and OOD for senior C-style embedded software engineers
    ... metaprogramming in C++ to whole different level using the template ... templates let the compiler do more work ... The worst thing is when a function throws an exception ... And of course, exception specifications should work like people think they work, not as they /do/ work. ...
    (comp.arch.embedded)
  • Re: Template Functions
    ... Templates need to be fully visible to the compiler when it generates ... When you don't have an explicit instantiation, ... it is unable to instantiate ... the template, i.e. create the code for that function. ...
    (microsoft.public.vc.language)
  • Re: Passing template struct as an argument to a template function
    ... > types could be used to instantiate templates? ... Let's say that some of my template functions, ... > compiler is in no way a good idea. ... Majority of libraries on the market are still template-free. ...
    (comp.lang.cpp)