Thread: Template functions

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

    Template functions

    I'm making these classes, Expression_Set and Variable_Set into a template class called Set. I have a function that looks like this:
    Code:
    template<typename T> void Set::expand_factor_expressions() {
      algo::for_each(members.begin(),members.end(),&T::expand_factor_expressions);
    }
    Here's my for_each function (declared in namespace algo):
    Code:
    template<typename _Iterator, typename _Function>
      _Function for_each(_Iterator a, _Iterator z, _Function t) {
        for (;a != z; ++a)
          ((*a).*t)();
        return t;
        
      }
    I can't compile expand_factor_expressions because one of the arguments needs to contain the template. Is there a way around this? I could expand for_each, but I'd rather look for a more elegant solution.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    That's a tough one. You may be able to dupe your compiler into making it work (but at the cost of uglying up your code a bit). Why not just use an old fassioned macro?

    [edit]
    Oops, I just realized that this is part of your template class. Stupid me.

    is your for_each function static?

    [/edit]
    Last edited by master5001; 06-24-2003 at 01:30 AM.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Can you pull expand_factor_expressions out of the classes and make them functors? You can then templatize them so you only need to say:

    algo::for_each(members.begin(),members.end(),expan d_factor_expressions<T>());

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    I solved it by defining the functions inside the class definition. Not the best solution, but it works...

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Why don't you explicitly give the call to for_each the template paramters (based off the type T of course).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 10:17 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. using Template Functions
    By ashcan1979 in forum C++ Programming
    Replies: 3
    Last Post: 09-20-2006, 12:44 AM
  5. Friend template functions
    By lyx in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2003, 01:11 PM