Is there a way to bind the <template> template parameter?
Context
I have a custom comparator that accepts another comparator and applies an additional check:
template <template <typename> class Comparator, typename T>
struct SoftOrder : public std::binary_function<T, T, bool> {
bool operator()(const T lhs, const T rhs) const {
return Comparator<T>()(lhs, rhs) && AnotherCheck();
}
};
I have a second class that takes a comparator, for example:
template <template <typename> class Comparator>
class Processor { ... };
It is easy to instantiate a Processorwith a standard comparator (e.g. std::less) as follows:
Processor<std::less> processor1;
Processor<std::greater> processor2;
However, it is not so easy to create an instance of c SoftOrder, since the compiler correctly complains about the missing argument of the second template:
Processor<SoftOrder<std::less> > processor3; // <-- Fails to compile
Current solutions
Before posting this question, I came up with several solutions.
The first solution is many derived classes
template <typename T>
struct SoftOrderLessThan : public SoftOrder<std::less, T> {};
template <typename T>
struct SoftOrderGreaterThan : public SoftOrder<std::greater, T> {};
The main disadvantage of this solution is the need to create a new structure every time a new option is required, for example:
template <typename T>
struct SoftOrderLessThan : public SoftOrder<std::less, T> {}; // Never used after the next line.
Processor<SoftOrderLessThan> processor3;
-
template <template <typename> class Comparator>
struct BindToSoftOrder {
template <typename T>
struct type : public SoftOrder<Comparator, T> {};
};
, :
Processor<BindToSoftOrder<std::less>::type> processor3;
, , , SoftOrder BindToSoftOrder, template<template<template>>>, .
- ++ 11
template <typename T>
using SoftOrderLessThan = SoftOrder<std::less, T>;
, , , , :
template <typename T>
using SoftOrderLessThan = SoftOrder<std::less, T>; // Never used again
Processor<SoftOrderLessThan> processor3;
,
?
Processor<SomeCoolMetaTemplateBind<SoftOrder, std::less>::type> processor3;
, , - Processor<boost::mpl::bind<SoftOrder, std::less> >, .
++ 03, ++ 11.
, , , .
+5