I need to choose between std::less and std::greater templates and store one in another template (I think I'm using the C++ STL vocab right..?). I have this:
template<typename> class stSortOrder;
stSortOrder = std::less;
if(sortby == "descending")
{
stSortOrder = std::greater;
}
Obviously it doesn't compile. I'm pretty sure it's cause I'm a relative novice at the STL.
CLARIFICATION:
I am implementing the accepted answer in this thread (the variation for public member functions).
Here's what I want to avoid repeating in a switch case:
void CSubscriptionItem::sortMonitoredItems( int nColumnIndex, Qt::SortOrder ulOrder )
{
switch(nColumnIndex)
{
case CMonitoredItem::NAME:
{
if(ulOrder == Qt::DescendingOrder)
{
qSort( m_qlpcMonitoredItems.begin(),
m_qlpcMonitoredItems.end(),
make_method_comparer<std::less>(&CMonitoredItem::getName) );
}
else
{
qSort( m_qlpcMonitoredItems.begin(),
m_qlpcMonitoredItems.end(),
make_method_comparer<std::greater>(&CMonitoredItem::getName) );
}
break;
}
I would like to replace std::less and std::greater in make_method_comparer<> with one template that is already set up depending on the sort order argument. This would really help reduce code size.
I've considered both of the answers posted, but they don't seem to work - likely because I am not too familiar with templates and I am simply using them incorrectly.