Click to See Complete Forum and Search --> : Function Templates within Class possible ??
quantass
January 13th, 2003, 12:01 AM
I've just discovered Function Templates and would like to implement it within my class. Is this possible?
I've tried doing something like this:
--
class MyClass
{
public:
template <class T> char WriteActionFile(T &incVar)
}
template <class T> char MyClass::WriteActionFile(T &incVar)
{
cout << sizeof T << endl;
}
void main(..)
{
MyClass myObj;
long val=10;
myObj.WriteActionFile(val);
}
--
What happens is VC++6 returns back the error message:
==
error C2893: Failed to specialize function template 'char __thiscall MyClass::WriteActionFile(T &)' - With the following template arguments:
'char'
==
Ok what did i do wrong? I've tried other combos but cant seem to get function templates to work for a specific class? Does this mean function templates can only be implemented within the global namespace? Ideally i would like to have the template prototype within the class declaration and the definition within the source file, that is, I would prefer not to have to inline the function body into the class declaration. :)
SeventhStar
January 13th, 2003, 01:46 AM
i think that when you implement the function you shouldn't whrite
template <class T>
this would work
char MyClass::WriteActionFile(T &incVar)
{
cout << sizeof T << endl;
}
Gabriel Fleseriu
January 13th, 2003, 02:21 AM
Bad news: Microsoft VC++ doesn't understand template member functions. They are in the Standard, though.
sagmam
January 13th, 2003, 03:26 AM
also in VC 6++. All you have to do, is impolement the function INLINE, like so:
class MyClass
{
template <class X> int func (X x) { ..... }
};
Gabriel Fleseriu
January 13th, 2003, 03:44 AM
Let's make it straight. VC++ will compile a class that has a template member function, but you will have a hard time to instantiate that member. The syntax you have to use in such a case is rather a rare one, and VC++ doesn't support it.
class A
{
public:
template<typename T>
size_t get_size(){return sizeof(T);}
};
int main()
{
A a;
size_t s = a.template get_size<int>(); //correct syntax, according to the Standard
return 0;
}
sagmam
January 13th, 2003, 04:16 AM
This is only true in your example, since get_size doesn't have any arguments.
If you run this code (compiled with VC++6):
class C
{
public:
template <class T>
int getsize(const T& x) { return sizeof(x); }
};
struct S
{
int x,y,z;
};
int main(int argc, char* argv[])
{
C c;
S s;
printf("%d\n", c.getsize(1));
printf("%d\n", c.getsize(1.5));
printf("%d\n", c.getsize(s));
return 0;
}
You'll get an output of:
4
8
12
galathaea
January 13th, 2003, 01:20 PM
Gabriel is correct in his assertion as to VC++'s support for template functions in general, however there are specific circumstances where function templates may be used. First, parametrized classes need only have their methods defined with the template parameter specified in the scope resolution, ie. something like
template <typename T>
ReturnType SomeClass<T>::SomeMethod(param list)
{ /* Something that uses T */ }
in the definition, and no parameter clarification is needed in the declaration (inside the class declaration).
Secondly, parametrized functions themselves must use the template parameters in their parameter list. This Knowledge Base article (http://support.microsoft.com/default.aspx?scid=kb;EN-US;240871) explains this in more detail and gives the specific compiler workaround of dummy arguments.
Finally, VC++ does not support general ordering of template specializations or even partial specializations (not applicable to parametrized functions anyway), and lookup may not be proper, so it is necessary to be careful when dealing with parametrized functions on the VC++ compiler to ensure proper behavior. Otherwise, for the specific OP problem, I would use the form I have given above in the code bracket...
I hope this helps somewhat...
[Edited the code snippet... silly me, I forgot the template keyword for name resolution...]
sagmam
January 14th, 2003, 01:29 AM
The article you mentioned is EXACTLY my point. the 'get_size' member did not have any arguments nor a return value which is template-based, so obviously the compiler doesn't know how to instantiate it when it's called.
Template specializations of members are only allowed inline.
And that's all there is to it :-)
mustvicky
February 17th, 2005, 03:04 AM
hello,
I am facing quite similar problem but with one difference. Some of the template functions are working and not producing compilation errors while some are not. Also, the template functions which are compiling does not contain the use of template variable T.
I am using VC++6
thanks in advance.
Regards
Vikas
sagmam
February 17th, 2005, 09:54 AM
If a template func doesn't use T arguments, the VS6 compiler will not be able to instantiate it. It's in the MSDN. Send a dummy T argument to solve this:
template <class T>
T func(T dummy = 0) {...}
I used the default value '0'. There is a drawback though: it REQUIRES that all T types used here will have a ctor that looks like this: T(int) {...}
This is true even if you use something other than 0, like NULL, which will require a ctor: T(void*) { ...} and so on.
If you don't want to use this trick, move to GNU or VS7 :-)
About the other thing you said, I didn't understand what compiles and what doesn't. Please send some code.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.