Declaring explicit specialization of member function.

J

jason.cipriani

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 T> void 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 T> void 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_<char> and 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
 
J

jason.cipriani

class A {
private:
template <typename T> void 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
 
B

Barry

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 T> void 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 T> void 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_<char> and 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-lite/templates.html#faq-35.12
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top