473,386 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

'Passing' an operator like <, >, == to a template function?

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

Aug 1 '05 #1
2 1121
Gundolf wrote:
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)?


Check out <functional> header and functors 'equal_to', 'not_equal_to', et
cetera.

V
Aug 1 '05 #2
* 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().


See std::equal_to and friends.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 1 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): ...
2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
0
by: ma740988 | last post by:
Consider #include <iostream> #include <string> #include <map> using namespace std; struct dstream // data_stream class {
8
by: Ook | last post by:
This is my code in it's entireity: #include <iostream> using namespace std; template <typename T> class SortedList { public: friend ostream& operator<< (ostream& os, const SortedList<T>&...
2
by: Arvid Requate | last post by:
Hello, I'd like to understand why the following code does not compile. It looks like a strangeness in connection with overload resolution for the <complex> header: The conversion operator...
1
by: grabit | last post by:
Hi Peoples i have a search page with a form field "subject" on my results page i have a paging routine . the first page lists its 10 records no trouble but when i click the "next" link i get a error...
1
by: ruchirp | last post by:
Hi, I've been trying to overload << for a function similar to boost lambda library. I want to make for_each(v.begin(), b.end(), cout << _1 << endl); to work, however, i'm getting a lot of problems...
4
by: aaragon | last post by:
Hi everyone, I was unable to find out why my code is not compiling. I have a template class and I'm trying to write the operator<< for standard output. Does anyone know why this is not right?...
4
by: =?ISO-8859-1?Q?Dar=EDo_Griffo?= | last post by:
I'm having an error with this code #include <iostream> template < typename Tclass TestOpTemplate { public: friend std::ostream& operator<< <>(std::ostream& os, const TestOpTemplate<T>& m);...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.