using template functions as part of a class? is it possible?
-
Hello, I need to use a template function, but I need that that template function be a class member... Let's say that I've tried to do:
INSIDE THE HEADER FILE:
template<class T> void MyClass::MyFunction(T& Parameter);INSIDE THE IMPLEMENTATION FILE:
template<class T> void CMyClass::MyFunction(T& Parameter)
{
SomeOperations...
}HOW DO I CALL THE FUNCTION:
bool b = false;
MyFunction(b);The fact is that using this code I get a lot of errors (one in each MyFunction call) like:
...MyFile.cpp(119) : error C2893: Failed to specialize function template 'void __thiscall CMyclass::MyFunction(T &)' With the following template arguments: 'bool'
The strange thing is that if I remove the declaration from the class (I've commented it out) and I call the function as a non class member, then it executes well... I need to be able to call that function while that function would be a part of CMyClass. Is this possible? and if it is possible, what do I'm doing wrong? Thank you in advance. NOTE: Somebody has told me that this is a weakness of the VC++ compiler, but I continue searching because I need it, what do you believe? -
Hello, I need to use a template function, but I need that that template function be a class member... Let's say that I've tried to do:
INSIDE THE HEADER FILE:
template<class T> void MyClass::MyFunction(T& Parameter);INSIDE THE IMPLEMENTATION FILE:
template<class T> void CMyClass::MyFunction(T& Parameter)
{
SomeOperations...
}HOW DO I CALL THE FUNCTION:
bool b = false;
MyFunction(b);The fact is that using this code I get a lot of errors (one in each MyFunction call) like:
...MyFile.cpp(119) : error C2893: Failed to specialize function template 'void __thiscall CMyclass::MyFunction(T &)' With the following template arguments: 'bool'
The strange thing is that if I remove the declaration from the class (I've commented it out) and I call the function as a non class member, then it executes well... I need to be able to call that function while that function would be a part of CMyClass. Is this possible? and if it is possible, what do I'm doing wrong? Thank you in advance. NOTE: Somebody has told me that this is a weakness of the VC++ compiler, but I continue searching because I need it, what do you believe?Firstly, the declaration you have there is not correct. ( or is it just formatting which removes the < typename T > bit ? ) Anyways, ( assuming it is not ), to declare a template member function you have to do this; in header:
template< typename T > void MyFunction( T& ); ... template< typename T > void MyClass::MyFunction( T & param ) { ... }
The point is that you can't define a templated method outside of the header-file where it is declared. ( The compiler might not complain, even 7 doesn't, but the linker looses it... ) Secondly, this will only partly work if you have anything earlier than VC 7. VC 6 will do it, however it might fail silently on certain cases which can cause you grief later. ( In particular if you try to specialise the member templates ) -=jarl=- -
Firstly, the declaration you have there is not correct. ( or is it just formatting which removes the < typename T > bit ? ) Anyways, ( assuming it is not ), to declare a template member function you have to do this; in header:
template< typename T > void MyFunction( T& ); ... template< typename T > void MyClass::MyFunction( T & param ) { ... }
The point is that you can't define a templated method outside of the header-file where it is declared. ( The compiler might not complain, even 7 doesn't, but the linker looses it... ) Secondly, this will only partly work if you have anything earlier than VC 7. VC 6 will do it, however it might fail silently on certain cases which can cause you grief later. ( In particular if you try to specialise the member templates ) -=jarl=- -
Firstly, the declaration you have there is not correct. ( or is it just formatting which removes the < typename T > bit ? ) Anyways, ( assuming it is not ), to declare a template member function you have to do this; in header:
template< typename T > void MyFunction( T& ); ... template< typename T > void MyClass::MyFunction( T & param ) { ... }
The point is that you can't define a templated method outside of the header-file where it is declared. ( The compiler might not complain, even 7 doesn't, but the linker looses it... ) Secondly, this will only partly work if you have anything earlier than VC 7. VC 6 will do it, however it might fail silently on certain cases which can cause you grief later. ( In particular if you try to specialise the member templates ) -=jarl=-...right, so reading, my reply again I realise that it might not be entirely clear....;P What I was trying to say was; Member function templates are poorly supported by VC 6 Member function templates are supported by VC 7, however... Member function template specialisation is not, ( not properly, anyways ), What you are trying to do, ( splitting template declaration and definition into separate files ), is the subject of the mysterious export keyword, which for numerous reasons has been deemed a red herring. Unfortunately (?), the only solution is to keep the declaration and definition in the same header file. If you want to make sure that the linker has a single instance of a template somewhere, ( and not rely on client code usage patterns ), you can instantiate it using the template keyword on its own, like so; in some cpp files somwhere...
template MyTemplatedClass;
this will generate all the code for MyTemplatedClas in the cpp file for the linker to use. HOWEVER....this doesn't work for template member functions, only for the whole class, ( or perhaps for free functions. ) So...'tis a bit messy... -=jarl=- -
...right, so reading, my reply again I realise that it might not be entirely clear....;P What I was trying to say was; Member function templates are poorly supported by VC 6 Member function templates are supported by VC 7, however... Member function template specialisation is not, ( not properly, anyways ), What you are trying to do, ( splitting template declaration and definition into separate files ), is the subject of the mysterious export keyword, which for numerous reasons has been deemed a red herring. Unfortunately (?), the only solution is to keep the declaration and definition in the same header file. If you want to make sure that the linker has a single instance of a template somewhere, ( and not rely on client code usage patterns ), you can instantiate it using the template keyword on its own, like so; in some cpp files somwhere...
template MyTemplatedClass;
this will generate all the code for MyTemplatedClas in the cpp file for the linker to use. HOWEVER....this doesn't work for template member functions, only for the whole class, ( or perhaps for free functions. ) So...'tis a bit messy... -=jarl=- -
Hello, I need to use a template function, but I need that that template function be a class member... Let's say that I've tried to do:
INSIDE THE HEADER FILE:
template<class T> void MyClass::MyFunction(T& Parameter);INSIDE THE IMPLEMENTATION FILE:
template<class T> void CMyClass::MyFunction(T& Parameter)
{
SomeOperations...
}HOW DO I CALL THE FUNCTION:
bool b = false;
MyFunction(b);The fact is that using this code I get a lot of errors (one in each MyFunction call) like:
...MyFile.cpp(119) : error C2893: Failed to specialize function template 'void __thiscall CMyclass::MyFunction(T &)' With the following template arguments: 'bool'
The strange thing is that if I remove the declaration from the class (I've commented it out) and I call the function as a non class member, then it executes well... I need to be able to call that function while that function would be a part of CMyClass. Is this possible? and if it is possible, what do I'm doing wrong? Thank you in advance. NOTE: Somebody has told me that this is a weakness of the VC++ compiler, but I continue searching because I need it, what do you believe?Try this approach:
// header
class CMyClass
{
public:
template void MyFunction(T& Parameter)
{
// implementation
}
};Although you can legally move the definition of
MyFunction
outside the body ofCMyClass
declaration, VC++ 6.0 crashes at it, so this is your only option (don't know about VC++ 7.0). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
Try this approach:
// header
class CMyClass
{
public:
template void MyFunction(T& Parameter)
{
// implementation
}
};Although you can legally move the definition of
MyFunction
outside the body ofCMyClass
declaration, VC++ 6.0 crashes at it, so this is your only option (don't know about VC++ 7.0). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo