Connecting Tech Pros Worldwide Help | Site Map

Member function templates in libraries

Arne Petersen
Guest
 
Posts: n/a
#1: Jul 22 '05
Hy,

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.
The VS compiler does not correctly bring together the function template
declaration and its instanciation.
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.
Now how can I get GNU version libraries be compiled under VS?

Help would be great, Arne Petersen
Arne Petersen
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Member function templates in libraries


Victor Bazarov schrieb:[color=blue]
>
> Arne Petersen wrote:[color=green]
> > Hy,[/color]
>
> Hy yourself.
>[color=green]
> >
> > 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.[/color]
>
> Let me understand, the template functions are instantiated in a cpp file
> that includes (among other things) the cpp file, right?
>[color=green]
> > The VS compiler does not correctly bring together the function template
> > declaration and its instanciation.[/color]
>
> What's "incorrect" about it? Could you be a bit more verbose here?
>[color=green]
> > 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.[/color]
>
> 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).
>[color=green]
> > Now how can I get GNU version libraries be compiled under VS?[/color]
>
> 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[/color]

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
Closed Thread