Template instantiation

This is a discussion on Template instantiation within the C++ Programming forums, part of the General Programming Boards category; Code: namespace algo { template<typename _Iterator, typename _Function, typename _Base> _Function for_each(_Iterator a, _Iterator z, _Function t, _Base base) { ...

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493

    Template instantiation

    Code:
    namespace algo {
      template<typename _Iterator, typename _Function, typename _Base>
      _Function for_each(_Iterator a, _Iterator z, _Function t, _Base base) {
        for (;a != z; ++a)
          base.t(*a);
        return t;
        
      }
      
    };
    My intention is to call this function many times like so from inside *this object:
    Code:
    algo::for_each(something.begin(),something.end(),add_if_not_zero,*this);
    Where add_if_not_zero is part of the same object *this is.
    Code:
    void clear_extra() {
      algo::for_each(something.begin(), something.end(), add_if_not_zero, *this);
    
    }
    Code:
    class Expression {
    public:
      std::vector<Something> something;
      void add_if_not_zero(const Term&);
      void clear_extra();
      //...
    };
    Here's my error:
    Code:
    expression.cpp: In member function `void Expression::clear_extra()':
    expression.cpp:15: assuming pointer to member `void
       Expression::add_if_not_zero(const Term&)'
    expression.cpp:15: (a pointer to member can only be formed with `&
       Expression::add_if_not_zero(const Term&)')
    expression.hpp: In function `_Function algo::for_each(_Iterator, _Iterator,
       _Function, _Base) [with _Iterator = __gnu_cxx::__normal_iterator<Term*,
       std::vector<Term, std::allocator<Term> > >, _Function = void
       (Expression::*)(const Term&), _Base = Expression]':
    expression.cpp:15:   instantiated from here
    expression.hpp:619: no matching function for call to `Expression::t(Term&)'
    make: *** [expression.o] Error 1
    I'm not sure what's wrong. Any clues? Ideas to improve for_each?

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    If I get this right, 't' is basically a callback function, right? Well, to have a callback function, you need to pass a pointer to the function. What the compiler is inferring about the types, however, is that _Function is not a pointer, but rather the function type itself.

    I haven't dealt with passing functions through as template parameters before (nor do I have a compiler with me to see if this would work), but perhaps 't' should be a _Function*.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Zach is right when he says you are using the function pointer incorrectly. You need to qualify the pointer you send as being part of the class. Also, the way you implement it is wrong.

    Code:
    #include <iostream>
    #include <vector>
    
    namespace yabba
    {
    	template <class N,class T, class U>
    	void for_each(N a,N z,T t,U u)
    	{
    		while(a != z)
    		{
    			(u->*t)(*a);
    			++a;
    		}	
    	}
    }
    
    
    template<class T>
    class foobar 
    {
    public:
      	void dohicky(T);
      	void whatever(const std::vector<T>&);
    };
    
    template<class T>
    void foobar<T>::dohicky(T i)
    {
    	std::cout << i << std::endl;
    }
     
    template<class T>
    void foobar<T>::whatever(const std::vector<T>& v)
    {
    	yabba::for_each(v.begin(),v.end(),&foobar::dohicky,this);
    }
    
    
    int main( void )
    {
    	std::vector<int> v;
    	v.push_back(1);
    	v.push_back(2);
    	v.push_back(3);
    	v.push_back(4);
    	
    	foobar<int> f;
    	f.whatever(v);
    	
    }

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    it works now, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template function instantiation
    By George2 in forum C++ Programming
    Replies: 21
    Last Post: 03-09-2008, 06:35 AM
  2. Replies: 6
    Last Post: 08-12-2007, 01:02 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21