Thread: Template functions

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002

    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:
    template<typename T> void Set::expand_factor_expressions() {
    Here's my for_each function (declared in namespace algo):
    template<typename _Iterator, typename _Function>
      _Function for_each(_Iterator a, _Iterator z, _Function t) {
        for (;a != z; ++a)
        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
    Visalia, CA, USA
    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?

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

    is your for_each function static?

    Last edited by master5001; 06-24-2003 at 01:30 AM.

  3. #3
    Registered User
    Join Date
    May 2003
    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
    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
    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