473,387 Members | 1,891 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,387 software developers and data experts.

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 1842
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
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...
0
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
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 $...
8
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 {}; ...
1
by: ranges22 | last post by:
****************************************************************** I am compiling a librarry which has a .h file containing th following:...
5
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...
3
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...
2
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
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.