Bug 15469 - Unable to compile valid code including transform() algorithm: <unknown type> is reported for the forth parameter
Summary: Unable to compile valid code including transform() algorithm: <unknown type> ...
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.3.3
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-05-15 21:01 UTC by relf
Modified: 2005-07-23 22:49 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description relf 2004-05-15 21:01:35 UTC
While compiling the code below, gcc 3.3.3 reports the error:

bug2.cpp:21: error: no matching function for call to `transform(
   std::_Rb_tree_iterator<std::pair<const int, int>, std::pair<const int,
   int>&, std::pair<const int, int>*>, std::_Rb_tree_iterator<std::pair<const
   int, int>, std::pair<const int, int>&, std::pair<const int, int>*>,
   __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >,
   <unknown type>)'

The code looks correct, not much different from the C++ Standard section 18.6.2
examples.
secondof() function by itself works fine - i've included a line into the code to
demonstrate that.

==============================

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

template <class cl1, class cl2>
const cl2& secondof(const pair<cl1,cl2>& p) {
    return p.second;
}

int main() {
    map<int,int> M;
    M[1] = 10;
    M[4] = 50;

    cout << "secondof() works as expected: " << secondof(*M.begin()) << endl;

    vector<int> V(M.size());

    transform(M.begin(),M.end(),V.begin(),secondof);
}
Comment 1 Andrew Pinski 2004-05-15 21:27:41 UTC
This is a dup of bug 5458.

*** This bug has been marked as a duplicate of 5458 ***
Comment 2 Wolfgang Bangerth 2004-05-17 02:39:50 UTC
No, the bug is actually something completely different. When you 
refer to 'secondof' in the call to transform, you reference a 
template without giving the compiler the chance to actually 
deduce the template arguments. You need to write this as 
follows: 
    transform(M.begin(),M.end(),V.begin(),secondof<int,int>); 
 
Wolfgang 
Comment 3 Wolfgang Bangerth 2004-05-17 02:40:28 UTC
As mentioned, this is invalid. 
Comment 4 relf 2004-05-17 04:26:30 UTC
Wolfgang, could you please explain the difference with the following example
from B.Stroustrup "The C++ Programming Language" 3rd edition, section 18.6.2:

template<class T> T* delete_ptr(T* p) { delete p; return 0; }
void purge(deque<Shape*>& s)
{
    transform(s.begin(),s.end(),s.begin(),delete_ptr);
    // ...
}

Note that here we pass just `delete_ptr' but not `delete_ptr<Shape>' as the
forth parameter. Why this code does not reqire explicit template class
specification while my code (that is not much different, in fact) does?
Comment 5 Gabriel Dos Reis 2004-05-17 07:27:07 UTC
Subject: Re:  Unable to compile valid code including transform() algorithm: <unknown type> is reported for the forth parameter

"relf at os2 dot ru" <gcc-bugzilla@gcc.gnu.org> writes:

| Wolfgang, could you please explain the difference with the following example
| from B.Stroustrup "The C++ Programming Language" 3rd edition, section 18.6.2:
| 
| template<class T> T* delete_ptr(T* p) { delete p; return 0; }
| void purge(deque<Shape*>& s)
| {
|     transform(s.begin(),s.end(),s.begin(),delete_ptr);
|     // ...
| }

Wolfgang is right.  We don't have types for address of function
templates or overload functions (not that a function template is
treated ini many cases as an infinite overload set).

| Note that here we pass just `delete_ptr' but not `delete_ptr<Shape>' as the
| forth parameter. Why this code does not reqire explicit template class
| specification while my code (that is not much different, in fact) does?

In my copy of TC++PL3, special edition, the code reads:

   struct Delete_ptr { // use function object to get inlining
        template<class T> T* operator()(T* p) { delete p; return 0; }
   };

   void purge(deque<Shape*>& s)
   {
        transform(s.begin(), s.end(), s.begin(), Delete_ptr());
   }

-- Gaby