4

I've got this class:

template <class T>
class list
{
    ...
    bool moreThan(T x, T y);
    bool lessThan(T x, T y);
    ...
};

I need a function pointer to change the behavior of my class, and switch between using bool moreThan(T, T) or bool lessThan(T, T). So I'm currently using:

bool (list<int>::*foo)(int x, int y);
foo = &list<int>::lessThan;

and to use it:

(this->*foo)(x, y);

but I would like to have a flexible function pointer, so that I can use it with whichever T I need, not just int. So is there a way to create a function pointer to a class template member? Something like:

template <class T>
bool (list<T>::*foo)(T x, T y); //this doesn't work
1
  • 3
    You're gonna struggle with this, because each specialisation of list may have a different definition of those functions, or even not define them at all! Commented Apr 18, 2015 at 3:09

3 Answers 3

2

No there isn't. A pointer-to-member has to point to something. A member function in a class template isn't a single function, it's a family of functions - and each one has a different type. There could even be a T for which list<T>::lessThan doesn't exist, or is a type or a variable!

If we made an alias for one such pointer to member:

template <typename T>
using CompFun = bool (list<T>::*)(T, T);

Then it's obvious that CompFun<int> and CompFun<string> are different types. You cannot create a generic CompFun<T> variable.

Depending on what it is you're trying to do, though, there might be a good way to accomplish that.

Sign up to request clarification or add additional context in comments.

6 Comments

I really wish you'd stop using this ambiguous term "method".
@LightningRacisinObrit What's ambiguous about it?
The term 'method' is not well-defined in C++. Did you mean to imply a function that is a member? Or one that is non-static? Or one that is virtual? Or one that is pure? To avoid misunderstandings, prefer standardized terminology: instead of `method' say 'function' prefixed with those specifiers that are relevant. We're not doing Java here.
@LightningRacisinObrit I take it you're not a fan of this question? Sure, "method" isn't a term in C++, but it's a term in OO. I use it to just mean function that is a member of a class (whether static/virtual/pure or not).
thanks, just one question: Do I need to declare the function pointer with list<int>:: to know that it's pointing to a function of the class list right? but why does it need to know?
|
2

Currently you have to do something like this:

template <typename T>
struct listComparePtr
{
    typedef bool (list<T>::*type)(T x, T y);
};

Use like this:

listComparePtr<int>::type foo = &list<int>::lessThan;
listComparePtr<double>::type foo2 = &list<double>::moreThan;

Comments

0

There's no such single function pointer, because a function pointer must point to a concrete address in memory, but each template instance will be a different function in a different place.

However, your attempt at a declaration is already a valid C++14 variable template. Example: http://coliru.stacked-crooked.com/a/b1f0904e0ee4d6ad

Similarly, you can put the templating into a C++14 generic lambda:

auto foo = []( auto && l, auto && x, auto && y )
             { return l.moreThan( x, y ); };

This has the advantage of giving you a handle which is a single type, although the function inside the lambda is still a template.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.