Instantiating template function by the address of operator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?ISO-8859-1?Q?Marcel_M=FCller?=

    Instantiating template function by the address of operator

    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(c onst 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<in t>,
    &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
  • Thomas J. Gritzan

    #2
    Re: Instantiating template function by the address of operator

    Marcel Müller wrote:
    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(c onst 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_boun d.
    { // 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<in t>,
    &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?

    --
    Thomas

    Comment

    • =?ISO-8859-1?Q?Marcel_M=FCller?=

      #3
      Re: Instantiating template function by the address of operator

      Thomas J. Gritzan wrote:
      Marcel Müller wrote:
      >Is the following code valid?
      >
      g++ warns about antique headers, so I replaced the includes with this:
      For testing purpose I didn't care about that.

      >// 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);
      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.
      >
      >// Returns true on match, pos receives the lower bound.
      >
      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.

      >template <class T>
      >int binary_search(c onst 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.
      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<in t>,
      >&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_compa rer
      { static int cmp(const T* elem, const T* key);
      { return elem->compareTo(*key );
      }
      };


      Marcel

      Comment

      Working...