简体   繁体   中英

template used in class involving a generic comparison function

I am trying to write a Bheap in templates and the insert function involving a generic comparison function. What is the usual way to do this? I know how to use function pointer in C. But Is there any typical C++ way to do that?

Someone told the first one, which class F could represent anything any function. But I want this function to be a comparison function which like f(T,T). While the second guy say something about the functors

  template <class T, class F>class Bheap
{
public:
    Bheap<T>(int allocateSize);
    void insert(T value, F f);
    void getMax();
private:
    int sizeUsed;
    int allocateSize;
    vector<T> myBheap;
};

A functor is a struct providing one or more overloads of operator (), which makes it a choice if you have several comparisons:

// Having a struct X { K key, V value };
struct Less {
    template <typename K, typename V>
    operator bool () (const X<K, V>& a, const X<K, V>& b> const { 
        return a.key < b.key;
    }
    template <typename K, typename V>
    operator bool () (const X<K, V>& a, const K& b> const { 
        return a.key < b;
    }
    template <typename K, typename V>
    operator bool () (const K& a, const X<K, V>& b> const { 
        return a < b.key;
    }
};

You should implement your class and the insert function, assuming whatever is passed is correct in terms of the number of arguments. If the number of arguments is not 2, the compiler will let you know.

This is the desired effect in these cases. Let the compiler detect that the function is not valid, and thus the user has to change the function to the correct requirements.

A demonstration using your code would be this:

#include <vector>

template <class T, class F>
class Bheap
{
public:
    Bheap(int allocateSize) {}
    void insert(T value, F f)
    {
        f(value, value);
    }
    void getMax();
private:
    int sizeUsed;
    int allocateSize;
    std::vector<T> myBheap;
};

void SomeFunction(int, int)
{}

int main()
{
    typedef void (*fn)(int, int);
    Bheap<int, fn> b(10);
    b.insert(10, SomeFunction);
}

You will see that this compiles and links correctly (I used a function pointer, but a functor with an overloaded operator() would also suffice).

Now if the user's function is changed to one that takes 1 argument, we get a different scenario:

#include <vector>

template <class T, class F>
class Bheap
{
public:
    Bheap(int allocateSize) {}
    void insert(T value, F f)
    {
        f(value, value);
    }
    void getMax();
private:
    int sizeUsed;
    int allocateSize;
    std::vector<T> myBheap;
};

void SomeFunction(int)
{}

int main()
{
    typedef void (*fn)(int);
    Bheap<int, fn> b(10);
    b.insert(10, SomeFunction);
}

The invalid function will make your class fail to compile. Since your template requires a 2 argument function, passing a 1 argument function via pointer causes the error (which you can see here: http://ideone.com/rT7RRa )


For function objects, here is an example of successful compilation:

http://ideone.com/yvWD5o

For unsuccessful compilation:

http://ideone.com/yjeAWB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM