Signature of function accepting functor as argument?

This is a discussion on Signature of function accepting functor as argument? within the C++ Programming forums, part of the General Programming Boards category; If I had a template class that contained lists of type <T>, what would the function's signature look like to ...

  1. #1
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168

    Signature of function accepting functor as argument?

    If I had a template class that contained lists of type <T>, what would the function's signature look like to pass functor objects that provide the sort semantics for the list?

    Code:
    
    void add_sort( // ?
    
    }
    to then be later called just like sorting any other list

    Code:
    
    // ...
    
    private_list.sort(previously_defined_functor);
    goto( comeFrom() );

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    What would add_sort() do?

  3. #3
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by C_ntua View Post
    What would add_sort() do?
    sorry, I want add_sort() to *store* the function pointer to be called at a later time.
    goto( comeFrom() );

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,114
    hmm... when you say "functor objects that provide the sort semantics", you are talking about functions that sort, rather than predicates used in a sort?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by laserlight View Post
    hmm... when you say "functor objects that provide the sort semantics", you are talking about functions that sort, rather than predicates used in a sort?
    I'm not sure. Functional programming in C++ is not one of my strengths, I'm admittedly intimidated by the syntax.

    Here's an example of what I'd like the end result to look like:
    Code:
    
    struct Custom_sort{
    
        bool operator()(const foo& a, const foo& b){
            return a.bar < b.bar;
        }
    } custom_sort;
    
    int main(){
    
        Custom_container<foo> foos;
        foos.add_sort(custom_sort);
    }
    where Custom_container<T> is my template container.

    EDIT: It's important that custom_sort not be called until needed by the Custom_container<T>. add_sort() just defines how one of the std::list<T> will be sorted, when it actually needs to be sorted.
    Last edited by StainedBlue; 06-01-2010 at 12:55 PM.
    goto( comeFrom() );

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,114
    Ah, then you are actually talking about a predicate. You can do something similiar to what std::set does:
    • The predicate type is a template parameter.
    • The predicate can be provided in a template constructor.
    • Otherwise, the predicate defaults to std::less<T>.

    You can then store the predicate as a member.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    So what's the advantage of using predicates over something like:

    Code:
    std::list<T> the_list;
    
    typedef bool (*list_sort)(T,T);
    std::list<list_sort> sort_routines;
    
    // ...
    
    void add_sort(bool (*ls)(T,T)){ sort_routines.push_back(ls); }
    
    // ...
    
    the_list.sort(*sort_routines[i]);
    what added functionality do they give you?
    goto( comeFrom() );

  8. #8
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Is the added benefit simply that functor objects can hold state?

    It seems like in many situations, you could just code around the limitations of mere functions, to avoid functor objects, which are clearly more difficult to code! for me anyway!
    goto( comeFrom() );

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    Why not just let your class/function take a template parameter? Ie
    Code:
    template<typename T>
    class myclass
    {
    ...
    T& stored_cmp_obj;
    };
    So long as the type supports your operators and functions, all works out fine. It needn't be more difficult.
    Though I am a little unsure of what exactly you are after, but I this is the principle you're after.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    So, just add them the way you do.
    Code:
    template<typename T> 
    void Custom_container::add_sort(bool (*ls)(T,T))
    { 
        sort_routines.push_back(ls);
        activeSort = ls;
    }
    ...
    the_list.sort(*sort_routines[i]);
    Or maybe you want to sort a list with the selected routine, something like:
    Code:
    template<typename T> 
    void Custom_container::select_sort(int index)
    { 
        //some checks
        activeSort = sort_routines[index];
    }
    ...
    myContainer.mySort(...);
    which can call sort with activeSort as a parameter for the given list you want. Except if I am missing something...

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    I would simply shorten it down to
    Code:
    template<typename T> 
    void Custom_container::add_sort(T ls)
    { 
        sort_routines.push_back(ls);
        ActiveSort = ls;
    }
    ...
    the_list.sort(sort_routines[i]);
    If T fails to have, say, operator (), it fails to compile. Simple.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM

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