473,395 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Member function templates in libraries

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
Jul 22 '05 #1
1 1727
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
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes...
3
by: Dave | last post by:
Hello all, I am trying to create a full specialization of a member function template of a class template. I get the following errors: Line 29: 'foo<T1>::bar' : illegal use of explicit...
7
by: Steven T. Hatton | last post by:
I am trying to convert some basic OpenGL code to an OO form. This is the C version of the program: http://www.opengl.org/resources/code/basics/redbook/double.c You can see what my current...
9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
2
by: Neelesh | last post by:
I wanted to know why member templates cannot be virtual - is it because there is a conceptual impossibility in having them, or is it because the current mechanism of implementaing virtual functons...
4
by: Joseph Turian | last post by:
Hi, What is the correct syntax to get the bar<T>::f<int, unsigned>() function to compile in the following fragment? Thanks, Joseph class foo {
8
by: Rahul | last post by:
Hi, Is there a way to partially specialize only a member function of a template class (not the whole class). e.g. template <typename A, typename B> class Base { public:
13
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.