|
-
June 21st, 2012, 11:56 PM
#1
template function
hi,
I just want to be sure..
I have made a template function as follow;
Code:
template<typename V>
ProcessValue(const & V)
{
//....
}
This is declared in my cpp file, so it is outside of my class now, and my class function is accessing this template function as if it is a glabol function. My question is should this "template function" be declared inside the class header just like any other c++ function as follow? But this doesn't support polymorphic!?
Code:
class MyClass
{
public:
template<typename V>
void ProcessValue(const & V);
}
void MyClass::ProcessValue(const & V data)
{
..............
}
-
June 22nd, 2012, 02:22 AM
#2
Re: template function
You declare a template in a .h file because they are compiled "when needed", as opposed to normal function which are "compiled now, and used later".
To support this, the compiler must know at all times the definition of the template, in case it would want to compile it. Because of this, the template MUST be declared in a .h file (or included in one).
Because compilers are smart, they understand that your intent was not to to actually inline the method, especially if it is virtual. It will create a full fledged symbol for your method, and call it polymorphically without problem.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
July 5th, 2012, 08:41 PM
#3
Re: template function
hi
I declared a template function in my .h file, but it doesn't seem to be able to take virtual.
Code:
template <typename K, typename V> virtual V GetWithDef(const std::map <K,V> & m, const K & key, const V & defval )
{
{
typename std::map<K,V>::const_iterator it = m.find( key );
if ( it == m.end() )
{
return defval;
}
else
{
return it->second;
}
}
}
Error 1 error C2898: 'V Test::GetWithDef(const std::map<K,V> &,const K &,const V &)' : member function templates cannot be virtual
-
July 6th, 2012, 11:20 AM
#4
Re: template function
 Originally Posted by mce
I declared a template function in my .h file, but it doesn't seem to be able to take virtual.
It's not clear from your code what you are actually trying to show.
Non-member functions cannot be virtual.
Template member functions also cannot be virtual. This is because a template (member) function actually defines a "family" of functions. Virtual functions work (in all known C++ compilers AFAIK) by storing a so-called vtable that stores pointers to the member functions that are resolved at run-time when a virtual function is called. When you instantiate a derived class, it will set the function pointer in the vtable to the overridden member function. That's a concept that cannot be scaled to a (possibly infinite) family of functions. Thus, template member functions cannot be virtual. For the same reason you also cannot take a pointer to a template function.
What is possible is what monarch_dodra mentioned, i.e. a template class can have a virtual (non-template) member function. Something like this
Code:
template <class T>
struct Base
{
virtual ~Base() {}
virtual T Foo() { return T(); }
};
struct Derived : public Base<int>
{
virtual int Foo() { return 1; }
};
Last edited by D_Drmmr; July 6th, 2012 at 11:23 AM.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|