473,799 Members | 2,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Declaring explicit specialization of member function.

Again I find myself bogged down in template syntax. Here I have a
class with a template member function. I know ahead of time what
template parameters I will be passing to the function. I do not want
to define the function in a header, I want to define it in a different
translation unit. Here is some code:

=== a.h ===

class A {
private:
template <typename Tvoid f_(T *); // the template...
public:
// ...will only be used with T = char and T = float.
void f (char *p) { f_<char>(p); }
void f (float *p) { f_<float>(p); }
};

=== a.cpp ===

#include "a.h"
#include <iostream>

// i want T=char and T=float code to be generated here.
template <typename Tvoid A::f_ (T *) {
std::cout << "f_: " << sizeof(T) << std::endl;
}

=== b.cpp ===

#include "a.h"

int main () {
A a;
char x;
float y;
a.f(&x);
a.f(&y);
return 0;
}

=== end ===

Compiling and linking the code, the template code is not generated and
there are undefined references to A::f_<charand A::f_<float>. What
is the syntax for declaring a member specialization, so I can force
that code to be generated? It does not seem to be enough to simply
call it from the A::f()'s.

Thanks,
Jason
Jun 27 '08 #1
2 1863
On May 26, 10:46 pm, "jason.cipri... @gmail.com"
<jason.cipri... @gmail.comwrote :
class A {
private:
template <typename Tvoid f_(T *); // the template...
public:
// ...will only be used with T = char and T = float.
void f (char *p) { f_<char>(p); }
void f (float *p) { f_<float>(p); }

};
Nevermind! It was a silly question, I got it. Moving the definition of
the two f()'s into a.cpp (instead of inlining them in a.h) causes the
template code to be generated like I want, and makes everything happy.

Thanks,
Jason
Jun 27 '08 #2
ja************@ gmail.com wrote:
Again I find myself bogged down in template syntax. Here I have a
class with a template member function. I know ahead of time what
template parameters I will be passing to the function. I do not want
to define the function in a header, I want to define it in a different
translation unit. Here is some code:

=== a.h ===

class A {
private:
template <typename Tvoid f_(T *); // the template...
public:
// ...will only be used with T = char and T = float.
void f (char *p) { f_<char>(p); }
void f (float *p) { f_<float>(p); }
};

=== a.cpp ===

#include "a.h"
#include <iostream>

// i want T=char and T=float code to be generated here.
template <typename Tvoid A::f_ (T *) {
std::cout << "f_: " << sizeof(T) << std::endl;
}

=== b.cpp ===

#include "a.h"

int main () {
A a;
char x;
float y;
a.f(&x);
a.f(&y);
return 0;
}

=== end ===

Compiling and linking the code, the template code is not generated and
there are undefined references to A::f_<charand A::f_<float>. What
is the syntax for declaring a member specialization, so I can force
that code to be generated? It does not seem to be enough to simply
call it from the A::f()'s.
http://www.parashift.com/c++-faq-lit...html#faq-35.12
--
Best Regards
Barry
Jun 27 '08 #3

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

Similar topics

4
1846
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it elaborate to make sure my questions are clear enough. Let's say I need to write a function whose logic is same for all types (T) except in the case of T * (including const T *). Furtheremore , the function needs to be written differently for...
0
1498
by: Patrick Kowalzick | last post by:
Dear all, the following code is illegeal (but compiles with MSVC 7.1): // *** illegal *** struct outer0 { template<typename inner_var> struct inner { }; template<>
19
2980
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $ Last-Modified: $Date: 2003/09/22 04:51:49 $ Author: Nicolas Fleury <nidoizo at gmail.com> Status: Draft Type: Standards Track
8
4804
by: Ferdi Smit | last post by:
I've never understood the rationale of allowing partial, but not explicit specialization for classes at non-namespace scope. Ie.: struct A { template <typename T1, typename T2> struct B {}; // this is not allowed: template <> struct B<int, float> {};
1
1995
by: ranges22 | last post by:
****************************************************************** I am compiling a librarry which has a .h file containing th following: ****************************************************************** template<typename T> void from_string(const char Str, T &Obj); template<> void from_string(const char Str, long &); // template<> void from_string(const char Str, unsigned lon &); //
5
2661
by: henkoo | last post by:
i want to explicit instantiate a member function of a class template, and got some error, can anyone give some advice to correct it? 3x complier: g++ 3.2 #include <iostream> #include <string> using namespace std;
3
4008
by: Steven T. Hatton | last post by:
Has anybody here used explicit instantiation of templates? Has it worked well? Are there any issues to be aware of? -- NOUN:1. Money or property bequeathed to another by will. 2. Something handed down from an ancestor or a predecessor or from the past: a legacy of religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF, from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
2
7703
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
0
2928
by: greek_bill | last post by:
Hi, I have a template function for which I use SFINAE to restrict one of the parameters. Then I also have a partial specialization of this function.I would like to provide an explicit instantiation of the partially specialized version, but my compiler (VC8) complains because it fails the SFINAE version. I just realized as I was typing this that I'm using partial _function_ specialization. I'm sure I remember reading somewhere that
0
9687
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
10482
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
10251
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
10225
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
10027
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...
1
7564
isladogs
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...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
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
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.