G
Gundolf
Hi,
I'm wondering if there is some neat way of passing a comparative
operator (<, >, = , <=, ...) to a template-style-function.
For starters, I guess we've all seen this neat little example:
template <class T> Min (T x, T y)
{
return x < y ? x : y;
}
Ok, but now suppose I wanted to use the _operator_ (here "<") as a
template instead - or even in addition to - the operand type:
bool template <operator O> comp (int x, int y)
{
return x O y;
}
So, having this I could say something like:
if (comp<=> (x, y)) ...
if (comp<!=> (x, y)) ...
and get the compiler to generate different versions of comp().
Of course I tried it and it doesn't compile
Can anybody think of a way of doing this (apart from using macros)?
Thanks,
Gundolf
I'm wondering if there is some neat way of passing a comparative
operator (<, >, = , <=, ...) to a template-style-function.
For starters, I guess we've all seen this neat little example:
template <class T> Min (T x, T y)
{
return x < y ? x : y;
}
Ok, but now suppose I wanted to use the _operator_ (here "<") as a
template instead - or even in addition to - the operand type:
bool template <operator O> comp (int x, int y)
{
return x O y;
}
So, having this I could say something like:
if (comp<=> (x, y)) ...
if (comp<!=> (x, y)) ...
and get the compiler to generate different versions of comp().
Of course I tried it and it doesn't compile
Can anybody think of a way of doing this (apart from using macros)?
Thanks,
Gundolf