LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-21-2007, 02:31 AM   #1
spursrule
LQ Newbie
 
Registered: Feb 2006
Distribution: LFS
Posts: 26

Rep: Reputation: 15
(C++) Possible to duplicate functionality of function pointers for templated fcns?


Assume I have a templated family of sort fcns such as

Code:
template <class Bi, class Cmp>  insertion_sort(Bi begin, Bi end, Cmp cmp);
template <class Bi, class Cmp>  quicksort(Bi begin, Bi end, Cmp cmp);
template <class Ran, class Cmp>  shellsort(Ran begin, Ran end, Cmp cmp);
...
where Bi and Ran are iterator types (bidirectional and random-access), and Cmp is a binary predicate function object class (such as less<T> from <functional>). Is it possible to set something equivalent to a fcn pointer, capable of calling any of these fcns?

I.e., in main I would check the command-line args, and say one specifies to do quicksort. I would like to do something along the lines of:

Code:
sort = &quicksort;
and then call the sort later (from the main fcn) as something like
Code:
// lt is an object of type std::less<T>
sort(&A[0],&A[n],lt)
- or -

Code:
// lst is a doubly-linked list
sort(lst.begin(),lst.end(),lt)
so that I don't have some long switch statement in my main fcn like:
Code:
switch(sort_type) {
case INSERTION:
  insertion_sort(&A[0],&A[n],lt);
  break;
case QUICK:
  quicksort(&A[0],&A[n],lt);
  break;
case SHELL:
  shellsort(&A[0],&A[n],lt);
  break;
...
}
What's a good way to do something like this? I.e., something equivalent to the idea of a templated fcn pointer sort, capable of handling whatever iterators are passed to it without knowing beforehand.

Can fcn objects be used to duplicate the functionality of generic fcn pointers (since I can't just create a templated fcn pointer)?

Last edited by spursrule; 12-21-2007 at 02:36 AM.
 
Old 12-22-2007, 04:26 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
There may be different ways of looking at this problem but I think that you will need to have a proper class hierarchy so that you iterators are specialised and that you have a sort function that expects a pointer to the the generalised version.
sort (Iter, Cmp)
and your iterators Bi and Ran inherit Iter. That way the signature of your functions will match and you should then be able to achieve what you require.
 
Old 12-23-2007, 04:36 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Do you want it to choose the algorithm automatically based on iterator type? You'll need some ugly compile-time template tricks for that, I'm afraid, since random-access sort functions won't compile with bidirectional iterators. You may need something like this:
Code:
#include <iterator>
#include <vector>
#include <list>

//fake sort functions for demo purposes

template <class Iterator> void random_access_sort(Iterator, Iterator, int(*)(const Iterator, const Iterator));

template <class Iterator> void bidirectional_sort(Iterator, Iterator, int(*)(const Iterator, const Iterator));


//partially-specialized class to determine sort type

template <class>
struct sort_function {};

//specialized for random-access iterators
template <>
struct sort_function <std::random_access_iterator_tag>
{
    template <class Iterator> static
    void sort(Iterator sStart, Iterator eEnd, int(*cComp)(const Iterator, const Iterator))
    { random_access_sort(sStart, eEnd, cComp); }
};

//specialized for bidirectional iterators
template <>
struct sort_function <std::bidirectional_iterator_tag>
{
    template <class Iterator> static
    void sort(Iterator sStart, Iterator eEnd, int(*cComp)(const Iterator, const Iterator))
    { bidirectional_sort(sStart, eEnd, cComp); }
};


//fake comparison function for demo purposes

template <class Iterator>
int less_than_compare(Iterator, Iterator);



//automatic sorting function which selects algorithm based on iterator type

template <class Iterator>
void auto_sort(Iterator sStart, Iterator eEnd)
{
    sort_function <typename std::iterator_traits <Iterator> ::iterator_category> ::
      sort(sStart, eEnd, &less_than_compare <Iterator>);
}

int main()
{
    std::vector <int> random_access;

    std::list <int>   bidirectional;

    //this uses the random-access sort function
    auto_sort(random_access.begin(), random_access.end());

    //this uses the bidirectional sort function
    auto_sort(bidirectional.begin(), bidirectional.end());
}
ta0kira

PS I think I probably misunderstood the question. Have you looked into using a functor? That would probably be the easiest way to get it done.

Last edited by ta0kira; 12-23-2007 at 04:49 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ templated Node class: pointers to different instantated class types jhwilliams Programming 3 08-20-2007 06:20 PM
Using C function pointers in C++ Hexstatic Programming 3 09-26-2006 05:58 PM
pointers to templated c++ functions gearoid_murphy Programming 11 08-11-2006 01:12 PM
Matrix of function pointers kvtournh Programming 9 03-20-2006 04:38 AM
Function pointers in C. Matir Programming 2 03-15-2005 03:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration