473,835 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1750
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(MySpecialCl ass&);
------------------------------------------

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<<"bla h"<<std::end l;
return (int)0;
}

//correct implicite instanciation
template<> int klasse::blah<in t>(int foo){
std::cout<<"int eger"<<std::end l;
return (int)1;
}

//incorrect explicit instanciation
template int klasse::blah(do uble 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
2789
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 as member variables. So including a forward declaration doesnt help, does it? Faced with these, I had the following options: -Include the appropriate header in the header file that contains the class definition that has a member variable that is...
3
2128
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 template arguments Line 29: 'bar' : unable to match function definition to an existing declaration What am I doing wrong?
7
2498
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 effort at converting that code to an OO form looks like in the code listed below. The problem I'm running into is that the OpenGL functions that take the names of other functions as arguments don't like the way I'm passing the member functions of my...
9
2324
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 same type for a given instance of the class, but different types for different instances.) #include<set> using namespace std; template<class T>
5
6594
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
2232
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 would not work with that concept? Assuming that a compiler doesnot support "export" keyword, I guess it is necessary that all instantiations of the member template must get the definition of the template (and hence the definition of the class...
4
3339
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
2443
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
6608
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 trying to use from an executable, compile using gcc 4.1.2. Everything works fine until I try specializing one of the static member function templates in a non-template class. I have a feeling I'm messing up something obvious so before I post a...
0
9803
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9652
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10808
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10520
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10560
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10233
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5804
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3088
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.