STL & template bug &

Alexandre Oliva oliva@dcc.unicamp.br
Sun Nov 2 12:45:00 GMT 1997


Miloslav Grundmann writes:

> // with -DTEMPLATE the compiler does not compile

And it is not expected to, actually.

> template <class T >
> inline bool compare(const Expr<T> a, const Expr<T> b){ return true; };

> sort( a.begin(), a.end(), compare );

How is the compiler supposed to know which version of the compare
function you want it to use?

You must explicitly specify the function type for this to build
successfully.  You may do this by casting compare to the appropriate
type:

  sort( a.begin(), a.end(),
        static_cast<bool (*)(const Expr<int>,const Expr<int>)>(compare) );

This compiles fine.  However, it seems to me that explicitly
specifying template arguments to compare should work:

  sort( a.begin(), a.end(), compare<int> );

test.cc:25: no matching function for call to `sort (int *, int *, {unknown type})'

It should also be possible to fix this problem by explicitly
specifying template arguments to sort:

  sort<vector<int>::iterator,
       pointer_to_binary_function<const Expr<int>, const Expr<int>, bool> >
    ( a.begin(), a.end(), compare );

test.cc:29: no matching function for call to `sort (int *, int *, {unknown type})'

Inserting a call to ptr_fun with the appropriate template types should
have worked too:

  sort( a.begin(), a.end(),
        ptr_fun<const Expr<int>, const Expr<int>, bool> (compare) );

test.cc:32: no matching function for call to `ptr_fun ({unknown type})'

Not even explicitly specifying template arguments for compare works:

  sort( a.begin(), a.end(),
        ptr_fun(compare<int>) );

test.cc:35: no matching function for call to `ptr_fun ({unknown type})'

Explicitly constructing an instance of
pointer_to_binary_function<const Expr<int>, const Expr<int>, bool>
should have worked too:

  sort( a.begin(), a.end(),
        pointer_to_binary_function<const Expr<int>, const Expr<int>, bool>(compare) );

test.cc:38: cannot resolve overloaded function `compare' based on non-function type

The failure becomes worse yet if an explicit template argument is
specified in the last example:

  sort( a.begin(), a.end(),
        pointer_to_binary_function<const Expr<int>, const Expr<int>, bool>(compare<int>) );

test.cc:41: sorry, not implemented: `tree_list' not supported by dump_decl
test.cc: In function `int main(...)':
test.cc:41: cannot resolve overloaded function `' based on non-function type

Even using just compare<>, instead of compare<int>, produces the same
failure.

  sort( a.begin(), a.end(),
        pointer_to_binary_function<const Expr<int>, const Expr<int>, bool>(compare<>) );

Just in case someone tries to fix these problems, I've attached source
code with the several variations I've tried.

-- 
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.cc
Type: text/x-c++
Size: 1313 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-bugs/attachments/19971102/4caf1687/attachment.bin>


More information about the Gcc-bugs mailing list