Re: Member function templates in libraries
From: Arne Petersen (apetersen_at_mip.informatik.uni-kiel.de)
Date: 08/11/04
- Next message: kackson: "How to convert from time to UTC?"
- Previous message: tom_usenet: "Re: Improper argument deduction???"
- In reply to: Victor Bazarov: "Re: Member function templates in libraries"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: kackson: "How to convert from time to UTC?"
- Previous message: tom_usenet: "Re: Improper argument deduction???"
- In reply to: Victor Bazarov: "Re: Member function templates in libraries"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|