Templated Function Typedef

This is a discussion on Templated Function Typedef within the C++ Programming forums, part of the General Programming Boards category; I have the following function: Code: template <int compareDirection> string getAbsoluteWord(Sequence &wordsList) ... I want to make two typedefs that ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    648

    Templated Function Typedef

    I have the following function:

    Code:
    template <int compareDirection>
    string getAbsoluteWord(Sequence &wordsList) ...
    I want to make two typedefs that define getSmallestWord as getAbsoluteWord<-1> and getLargestWord as getAbsoluteWord<1>. The following gives me compile errors.

    Code:
    typedef getAbsoluteWord <-1> getSmallestWord;
    typedef getAbsoluteWord <1> getLargestWord;
    I've tried a myriad of different syntaxes, including those for function pointers. I've also tried google with no luck.

    Is there any way to do this?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Functions aren't types, so you can't use typedef.

    First, though, is the direction as a compile-time parameter a good idea? I doubt it.

    Anyway, you can make wrapper functions:
    Code:
    inline string getSmallestWord(Sequence &wordsList)
    {
      return getAbsoluteWord<-1>(wordsList);
    }
    Nevertheless, I recommend you make the compare direction a runtime parameter.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Thanks for the explanation. I was using the preprocessor and totally forgot about using inline functions for that purpose.

    I'm using a template parameter for that simply because it would force the compiler to make two versions of the function for performance's sake. I know it's generally bad practice but this program is small and won't matter. In larger programs, I can see the readability problems.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    It's not so much a readability problem; I simply think that if you implement it correctly, the slowdown will be unnoticeable, and you have the advantage of being able to decide at runtime which mode you want.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Making it a compile time parameter seems like a decent enough idea. Let the compiler optimize it. It doesn't appear to really result in nastier code. Look at something like a std::map where you can give the comparison in the template.
    Now if this optimization results in nastier code to write, I would disagree with what I just said.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Because I was bored: You can use typedefs if you typedef some function pointers:
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    
    using namespace std;
    
    template <int A>
    string someFunc(double b)
    {
            ostringstream oss;
            oss << "Int a = " << A << "  but b = "<<b;
            return oss.str();
    }
    
    typedef string (*someFuncPtr)(double);
    
    someFuncPtr Bob = someFunc<5>, Bill = someFunc<-3>;
    
    int main()
    {
            cout << Bob(10.328) << '\n' << Bill(30.4) << endl;
    }

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You could use a define:
    Code:
    #define getSmallestWord getAbsoluteWord<-1>
    But maybe there's a good reason not to do that...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    I was using a preprocessor statement similar to yours except with parenthesis, however, I think that the pure C++ approach with inline functions is much more in the nature of good coding. The typedef with the function pointer came to mind actually but I decided not to do it because it's kinda messy with the extra variables and stuff.

    The compile-time template parameter versus a runtime parameter is really a choice dependant on the usage of the function. This is a simple dumb little program for my C++ class I'm forced to take (there's no placement test and I already skipped the first two courses in the programming track). The templated function impressed a ton of people. And yeah, the speed difference is neglible too. It's probably better to make it a runtime parameter but who really cares.

    If anybody is interested, here's that section of the code with tons of comments taken out for breivity. As you can tell, the implementation isn't messy at all.

    Code:
    // Returns the alphabetical absolute word of the sequence, depending on the direction.
    //   <compareDirection>: Positive stands for the greatest word, negative stands for the smallest word, alphabetically.
    //   wordsList: The sequence of words to process.
    template <int compareDirection>
    static string getAbsoluteWord(Sequence &wordsList)
    {
      // Set the first word of the sequence as the absolute word.
      string absoluteWord = wordsList[0];
    
      // Loop through all the words past the first one.
      int count = wordsList.size();
      for (int i = 1; i < count; ++i)
      {
        const string &currentWord = wordsList[i];
        int comparison = currentWord.compare(absoluteWord) * compareDirection;
    
        // If this word is "greater" than the current absolute word, then it should be the new absolute word.
        if (comparison > 0)
        {
          absoluteWord = currentWord;
        }
      }
    
      return absoluteWord;
    }
    
    
    // Returns the smallest word in the sequence.
    //   wordsList: The sequence of words to process.
    static inline string getSmallestWord(Sequence &wordsList)
    {
      return getAbsoluteWord <-1> (wordsList);
    }
    
    
    // Returns the largest word in the sequence.
    //   wordsList: The sequence of words to process.
    static inline string getLargestWord(Sequence &wordsList)
    {
      return getAbsoluteWord <1> (wordsList);
    }
    Last edited by Speedy5; 09-19-2005 at 07:28 PM.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Quote Originally Posted by JaWiB
    But maybe there's a good reason not to do that...
    You're using the preprocessor and there's a different way. Reason enough for me, anyway.
    OK, for the sake of absurdity, here's an obscure example that fails. There's a template class that takes, as a type parameter, a function pointer:
    Code:
    template < string(*)(Sequence&) >
    class Whee;
    Now he wants to instantiate:
    Code:
    Whee<getSmallestWord> obj;
    The CPP transforms this to:
    Code:
    Whee<getAbsoluteWord<-1>> obj;
    Which, as everyone knows, is invalid.


    OK, just looked at the code. It really wouldn't make a difference in speed. That's one memory access more, at most.
    Last edited by CornedBee; 09-20-2005 at 04:16 AM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Passing a function pointer to a templated type
    By skorman00 in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2004, 08:31 PM
  5. Replies: 5
    Last Post: 02-08-2003, 06:42 PM

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