CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2002
    Posts
    788

    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)
    {
    ..............
    
    }

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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.

  3. #3
    Join Date
    Jul 2002
    Posts
    788

    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

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: template function

    Quote Originally Posted by mce View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured