Re: Member function templates in libraries

From: Arne Petersen (apetersen_at_mip.informatik.uni-kiel.de)
Date: 08/11/04


Date: Wed, 11 Aug 2004 12:00:51 +0200

Victor Bazarov schrieb:
>
> 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

Thanks for reply!

The problem I have is that the function temlpate is a class member. The
template specialization (for int) is instanciatet correctly but the
explicit instanciation (for double) for the generic template goes wrong.

----------------------------hello1.h
class klasse {

public:
    klasse(){};

    template<class T> int blah(T foo);
    template <> int blah<int>(int foo);
};
----------------------------hello1.h

----------------------------hello1.cpp
#include "hello1.h"

#include <iostream>

template<class T> int klasse::blah(T foo){
        std::cout<<"blah"<<std::endl;
        return (int)0;
}

//correct implicite instanciation
template<> int klasse::blah<int>(int foo){
        std::cout<<"integer"<<std::endl;
        return (int)1;
}

//incorrect explicit instanciation
template int klasse::blah(double foo);
----------------------------hello1.cpp

Maybe this problem depends on the used compiler. So if you know a good
ngroup for the Visual Studio .NET compiler I would be thankfull.

Arne



Relevant Pages

  • 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: OOP and OOD for senior C-style embedded software engineers
    ... metaprogramming in C++ to whole different level using the template ... In particular, templates let the compiler do more work at compile time, and save the target from work at run time. ... Templates can be great for generic libraries, and do not necessarily create code bloat. ... You can use RAII even without exceptions, but obviously you have to handle your errors explicitly. ...
    (comp.arch.embedded)
  • Re: [C++] Vector - guaranteed to be contiguous?
    ... >> member is templated on the input iterators as well. ... template<typename OUTITER, typename INITER, typename INITERT> ... What happens is that the compiler sees copy and thinks, "Hey, I know copy. ... But it is a template, let me try to deduce those types. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: generic programming is very powerful. c should support it
    ... Where the compiler matches the correct template. ... already happens differently on different implementations. ...
    (comp.lang.c)