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
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