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 bunch of code does the following
look correct? Thanks.
--- Shared Library (Arrays.h) ---
namespace example {
class Arrays {
public:
template<class Tstatic int compareItems(T *a1, T *a2, int length);
protected:
Arrays() {};
};
template<class Tint Arrays::compare Items(T *a1, T *a2, int length) {
printf("Calling generic function.\n");
return 1;
}
template<int Arrays::compare Items<char>(cha r *a1, char *a2, int
length) {
printf("Calling char specialization. \n");
return 0;
}
} 13 6596
On 2007-10-14 18:38, mike b wrote:
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 bunch of code does the following
look correct? Thanks.
--- Shared Library (Arrays.h) ---
namespace example {
class Arrays {
public:
template<class Tstatic int compareItems(T *a1, T *a2, int length);
protected:
Arrays() {};
};
template<class Tint Arrays::compare Items(T *a1, T *a2, int length) {
printf("Calling generic function.\n");
return 1;
}
template<int Arrays::compare Items<char>(cha r *a1, char *a2, int
length) {
printf("Calling char specialization. \n");
return 0;
}
}
I can see nothing wrong with it (except using printf and not including
<stdio.h>). What error messages are you getting and how are you trying
to use the function?
--
Erik Wikström
On Oct 14, 12:38 pm, mike b <michaeljber... @gmail.comwrote :
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 bunch of code does the following
look correct? Thanks.
--- Shared Library (Arrays.h) ---
namespace example {
class Arrays {
public:
template<class Tstatic int compareItems(T *a1, T *a2, int length);
protected:
Arrays() {};
};
template<class Tint Arrays::compare Items(T *a1, T *a2, int length) {
printf("Calling generic function.\n");
return 1;
}
template<int Arrays::compare Items<char>(cha r *a1, char *a2, int
length) {
printf("Calling char specialization. \n");
return 0;
}
}- Hide quoted text -
- Show quoted text -
Your specialization should be inside the example:Arrays class, not
outside it.
namespace example {
class Arrays {
public:
template<class Tstatic int compareItems(T *a1, T *a2, int
length);
static int compareItems(ch ar*a1, char *a2, int
length);
};
You cant add a new specialization once the class declaration is
closed. If compareItems were a namespace level declaration, then you
can reopen the namespace and add a new specialization later.
Lance
mike b wrote:
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 bunch of code does the following
look correct? Thanks.
--- Shared Library (Arrays.h) ---
namespace example {
class Arrays {
public:
template<class Tstatic int compareItems(T *a1, T *a2, int length);
protected:
Arrays() {};
};
template<class Tint Arrays::compare Items(T *a1, T *a2, int length) {
printf("Calling generic function.\n");
return 1;
}
template<int Arrays::compare Items<char>(cha r *a1, char *a2, int
length) {
printf("Calling char specialization. \n");
return 0;
}
}
This below compiles fine and does what I think you expect. What problem
are you having ?
#include <cstdio>
#include <iostream>
namespace example {
class Arrays {
public:
template<class Tstatic int compareItems(T *a1, T *a2, int length);
Arrays() {};
};
template<class Tint Arrays::compare Items(T *a1, T *a2, int length) {
std::printf("Ca lling generic function.\n");
return 1;
}
template<int Arrays::compare Items<char>(cha r *a1, char *a2, int
length) {
std::printf("Ca lling char specialization. \n");
return 0;
}
}
int main()
{
std::cout << example::Arrays ::compareItems( (short*)0, (short*)0, 0
) << "\n";
std::cout << example::Arrays ::compareItems( (char*)0, (char*)0, 0 )
<< "\n";
}
On 2007-10-14 20:16, Lance Diduck wrote:
On Oct 14, 12:38 pm, mike b <michaeljber... @gmail.comwrote :
>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 bunch of code does the following look correct? Thanks.
--- Shared Library (Arrays.h) ---
namespace example { class Arrays { public: template<class Tstatic int compareItems(T *a1, T *a2, int length); protected: Arrays() {};
};
template<cla ss Tint Arrays::compare Items(T *a1, T *a2, int length) { printf("Calling generic function.\n"); return 1;
}
template<int Arrays::compare Items<char>(cha r *a1, char *a2, int length) { printf("Calling char specialization. \n"); return 0; } }- Hide quoted text -
- Show quoted text -
Your specialization should be inside the example:Arrays class, not
outside it.
namespace example {
class Arrays {
public:
template<class Tstatic int compareItems(T *a1, T *a2, int
length);
static int compareItems(ch ar*a1, char *a2, int
length);
};
Actually, what you are suggesting is not a specialisation, but declaring
a non-parametrised function taking care of the special case. Which, to
my knowledge, will work just as well but there are probably some subtle
details that make specialised parametrised functions different from
normal functions.
--
Erik Wikström
On Oct 14, 11:16 pm, Lance Diduck <lancedid...@ny c.rr.comwrote:
On Oct 14, 12:38 pm, mike b <michaeljber... @gmail.comwrote :
Your specialization should be inside the example:Arrays class, not
outside it.
namespace example {
class Arrays {
public:
template<class Tstatic int compareItems(T *a1, T *a2, int
length);
static int compareItems(ch ar*a1, char *a2, int
length);
};
IIRR, a template specialization will always start with the word
"template". Hence, the above (non-template) function is surely not a
specialization of the template function.
>
You cant add a new specialization once the class declaration is
closed.
14.7.3(2) says:
"An explicit specialization of a member function, member class or
static data member of a class template shall be declared in the
namespace of which the class template is a member"
Which means that new specializations can be added in the same
namespace.
-N
On Oct 14, 2:16 pm, Lance Diduck <lancedid...@ny c.rr.comwrote:
On Oct 14, 12:38 pm, mike b <michaeljber... @gmail.comwrote :
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 bunch of code does the following
look correct? Thanks.
--- Shared Library (Arrays.h) ---
namespace example {
class Arrays {
public:
template<class Tstatic int compareItems(T *a1, T *a2, int length);
protected:
Arrays() {};
};
template<class Tint Arrays::compare Items(T *a1, T *a2, int length) {
printf("Calling generic function.\n");
return 1;
}
template<int Arrays::compare Items<char>(cha r *a1, char *a2, int
length) {
printf("Calling char specialization. \n");
return 0;
}
}- Hide quoted text -
- Show quoted text -
Your specialization should be inside the example:Arrays class, not
outside it.
namespace example {
class Arrays {
public:
template<class Tstatic int compareItems(T *a1, T *a2, int
length);
static int compareItems(ch ar*a1, char *a2, int
length);
};
You cant add a new specialization once the class declaration is
closed. If compareItems were a namespace level declaration, then you
can reopen the namespace and add a new specialization later.
Lance
I tried this but it doesn't automatically call my specialization. Do
I need to call overloaded implementations of the function from inside
the generic implementation? So basically don't use specialization at
all, use overloading from within the single generic implementation?
On Oct 14, 2:16 pm, Gianni Mariani <gi4nos...@mari an.wswrote:
mike b wrote:
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 bunch of code does the following
look correct? Thanks.
--- Shared Library (Arrays.h) ---
namespace example {
class Arrays {
public:
template<class Tstatic int compareItems(T *a1, T *a2, int length);
protected:
Arrays() {};
};
template<class Tint Arrays::compare Items(T *a1, T *a2, int length) {
printf("Calling generic function.\n");
return 1;
}
template<int Arrays::compare Items<char>(cha r *a1, char *a2, int
length) {
printf("Calling char specialization. \n");
return 0;
}
}
This below compiles fine and does what I think you expect. What problem
are you having ?
#include <cstdio>
#include <iostream>
namespace example {
class Arrays {
public:
template<class Tstatic int compareItems(T *a1, T *a2, int length);
Arrays() {};
};
template<class Tint Arrays::compare Items(T *a1, T *a2, int length) {
std::printf("Ca lling generic function.\n");
return 1;
}
template<int Arrays::compare Items<char>(cha r *a1, char *a2, int
length) {
std::printf("Ca lling char specialization. \n");
return 0;
}
}
int main()
{
std::cout << example::Arrays ::compareItems( (short*)0, (short*)0, 0
) << "\n";
std::cout << example::Arrays ::compareItems( (char*)0, (char*)0, 0 )
<< "\n";
}
The problem only occurs when code is separated into multiple binaries,
a shared lib and executable, with multiple users in the app.
mike b wrote:
On Oct 14, 2:16 pm, Gianni Mariani <gi4nos...@mari an.wswrote:
....
The problem only occurs when code is separated into multiple binaries,
a shared lib and executable, with multiple users in the app.
Are definitions of all template functions in every compilation unit ?
(i.e. included into every binary's source in question ?) If not, that's
your problem.
On Oct 14, 6:13 pm, Gianni Mariani <gi4nos...@mari an.wswrote:
mike b wrote:
On Oct 14, 2:16 pm, Gianni Mariani <gi4nos...@mari an.wswrote:
...
The problem only occurs when code is separated into multiple binaries,
a shared lib and executable, with multiple users in the app.
Are definitions of all template functions in every compilation unit ?
(i.e. included into every binary's source in question ?) If not, that's
your problem.
I defined my entire template class in the header file (.h) so it
should be making it into every compilation unit. Is there something
aside from that I need to do? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jee |
last post by:
Hi,
Does Visual C++ support member function template? I have the
following code which can not get through VC6
compiler.
In header,
class myclass{
public:
template<class T>
static std::vector<T> getIntersection(std::vector<T>& ,
|
by: 0to60 |
last post by:
I don't know if I have that terminology right, but does anyone know if
static member functions (or free standing functions for that matter) are any
less overhead than actual member functions that operate on an instance of
that class?
I'm writing a math calculator that responds to real time data and it needs
to be FREAKY fast. To minimize the number of arguments that certain methods
take, I have stored some data within a class and made...
|
by: Joost Ronkes Agerbeek |
last post by:
Why is it allowed in C++ to call a static member function of an object
through an instance of that object? Is it just convenience?
tia,
Joost Ronkes Agerbeek
|
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?
|
by: SJ |
last post by:
Hi:
I have a class which has a static member function. The function
implements something common to all instances.
How can the static member function know all of the (Get access to the
instances' handles) instances?
Thanks in advance for any help
| |
by: ajay.sonawane |
last post by:
How to call static member function of template class.
For example
template <typename T>
class A
{
private:
int m_i;
|
by: Simon G Best |
last post by:
Hello!
I'm trying to specialize a member function template of a class template,
like this:-
template<typename Tclass thingy {
public:
template<typename UT f (const U &) const;
};
|
by: chsalvia |
last post by:
In generic programming, static member functions and functors seem to
be very useful. I've noticed that some libraries take the approach of
using templated static member functions to provide generic
functionality, while others use functors. Take for example the
std::string char_traits class. Here, a static member "eq" is defined
to serve as a generic binary comparison function.
My implementation defines it as;
static bool eq(const...
|
by: subramanian100in |
last post by:
why can't a static member function be declared as const ? We can
declare a non-static member function as const, to indicate that it
does not modify the non-mutable data members. In the same way, is
there a provision to indicate that a static member function does not
modify the static data members but only uses their values ?
Kindly explain.
Thanks
V.Subramanian
|
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,...
|
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,...
| |
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
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...
| |