Instantiating template function by the address of operator

M

Marcel Müller

Is the following code valid?


#include <stdlib.h>
#include <ostream.h>

// compare *l to *r
template <class T>
int comparer(const T* l, const T* r)
{ if (*l < *r)
return -1;
return *l == *r;
}

// Returns true on match, pos receives the lower bound.
template <class T>
int binary_search(const T* data, size_t len, int (*fcmp)(const T* elem,
const T* key), const T* key, size_t& pos)
{ // does not care
pos = 0;
return 0;
}

int foo(int key)
{ const int arr[] = {1,2,3,4};
size_t pos;
return binary_search(&*arr, sizeof arr/sizeof *arr, &comparer<int>,
&key, pos)
^
? pos : ~pos;
}

int main()
{ cout << foo(3);
return 0;
}


One of my compilers says
test2.cpp(24:68) : error EDC3090: Syntax error - expected "(" and
found ">".
at the marked position.


Marcel
 
T

Thomas J. Gritzan

Marcel said:
Is the following code valid?

g++ warns about antique headers, so I replaced the includes with this:
#include <stdlib.h>
#include <ostream> // there's no ostream.h
#include <iostream> // cout is in <iostream>
using namespace std;

Some comments about the code below.
#include <stdlib.h>
#include <ostream.h>

// compare *l to *r
template <class T>
int comparer(const T* l, const T* r)
{ if (*l < *r)
return -1;
return *l == *r;
}

Prefer references oder pointers. They are safer to use:

template <typename T>
int comparer(const T& l, const T& r);

Also, your comparer might not return the right results. It returns 1 on
equality and 0 and greater-than. If you want strcmp-like return values,
you would have to change == to != in the return statement.

For a binary search algorithm, it might be faster and easier just to use
a less-than comparator. Almost all of the standard library works just
with std::less.
// Returns true on match, pos receives the lower bound.

If the return values can be true and false, the type should be a bool.
template <class T>
int binary_search(const T* data, size_t len, int (*fcmp)(const T* elem,
const T* key), const T* key, size_t& pos)

Consider using a iterator pair instead of a begin pointer and a length.

key should be passed by value or by const reference.

There might be no easy way to get the actual position of the lower
bound, if this function calls itself recursivly. So returning an
iterator (or pointer) to the position could be a better way.
Read about std::lower_bound.
{ // does not care
pos = 0;
return 0;
}

int foo(int key)
{ const int arr[] = {1,2,3,4};
size_t pos;
return binary_search(&*arr, sizeof arr/sizeof *arr, &comparer<int>,
&key, pos)
^
? pos : ~pos;
}

int main()
{ cout << foo(3);
return 0;
}


One of my compilers says
test2.cpp(24:68) : error EDC3090: Syntax error - expected "(" and
found ">".
at the marked position.

Both Comeau and g++ (with -Wall -ansi -pedantic) compiles it fine.

How old is your compiler?
 
M

Marcel Müller

Thomas said:
g++ warns about antique headers, so I replaced the includes with this:

For testing purpose I didn't care about that.

Prefer references oder pointers. They are safer to use:

template <typename T>
int comparer(const T& l, const T& r);

I think that makes mo much difference. const T* is pretty much the same
that const T&, except for the syntax.

Also, your comparer might not return the right results. It returns 1 on
equality and 0 and greater-than.

Your are right. Again I only did it for testing. It is not called in the
example anyway.

For a binary search algorithm, it might be faster and easier just to use
a less-than comparator. Almost all of the standard library works just
with std::less.


If the return values can be true and false, the type should be a bool.

Don't know where the int came from. I think from a test with another
very old compiler that does not know about bool. In the original coding
it is bool.

Consider using a iterator pair instead of a begin pointer and a length.

The real coding passes an array class with T** and size_t internally.
(All T are reference types here.) But that requires a bunch of
additional types and do not belong to the question.

key should be passed by value or by const reference.

It is a wrapper to a C library, so I have no choice.

There might be no easy way to get the actual position of the lower
bound, if this function calls itself recursivly.

The common implementation of binary search in a random access container
does not use recursion.

int foo(int key)
{ const int arr[] = {1,2,3,4};
size_t pos;
return binary_search(&*arr, sizeof arr/sizeof *arr, &comparer<int>,
&key, pos)
^
? pos : ~pos;
}

One of my compilers says
test2.cpp(24:68) : error EDC3090: Syntax error - expected "(" and
found ">".
at the marked position.

Both Comeau and g++ (with -Wall -ansi -pedantic) compiles it fine.

How old is your compiler?

Too old on the face of it. The executables are from '97.


I use a work around for now:

template <class T>
struct interface_comparer
{ static int cmp(const T* elem, const T* key);
{ return elem->compareTo(*key);
}
};


Marcel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,255
Latest member
TopCryptoTwitterChannels

Latest Threads

Top