Can you use templates (or the like) in C ++ to indicate which operation is performed in a function?
I donβt know how to explain this more clearly, so I will show you how this could be (but not done) in the code:
template <operator OPERATION> int getMaxOrMin(int a, int b) { return a OPERATION b ? a : b; }
where finding the maximum or minimum of a or b will be (this is where my pseudo-syntax gets a little confused, carry me):
int max = getMaxOrMin< > > (a, b); int min = getMaxOrMin< < > (a, b);
I know that itβs not how to do it at all (because it doesnβt even make sense syntactically), but I hope this clarifies the type of thing I want to do.
The reason for me is that I am doing a PriorityQueue implementation, and it would be nice to easily switch between a base, which is a maximum heap or a mini-heap on the fly, without copying and pasting the code to make two different classes.
I know that I could do this with a macro, but the only way I knew how to do this would give me either the maximum heap or the mini-heap, but not both in the same compilation. I probably don't notice the way.
source share