469,958 Members | 1,914 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,958 developers. It's quick & easy.

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 1731
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Patrick Kowalzick | last post: by
19 posts views Thread by Nicolas Fleury | last post: by
3 posts views Thread by Steven T. Hatton | last post: by
reply views Thread by greek_bill | last post: by
reply views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.