C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

transform - User Error or Compiler Error

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
andrew.bell.ia@gmail.com
Guest





PostPosted: Wed Sep 21, 2005 4:12 pm    Post subject: transform - User Error or Compiler Error Reply with quote



Hi,

I get a compiler error when trying to use std::transform to
iteratate/modify a list. If I write the same code by hand, it compiles
fine. Am I misusing transform, or is this a compiler bug? The types
involved are complex templates, but I don't think this should have any
bearing on things.

-------------------------------

const UserIFSeq& uList;
UserIFSeq::const_iterator i;
UserISeq users;

//
// Compiles Fine.
//
for (i = uList.begin(); i != uList.end(); i++)
users.push_back(UserIPrx::uncheckedCast(*i));
//
// Produces Error.
//
std::transform(uList.begin(), uList.end(), back_inserter(users),
UserIPrx::uncheckedCast);

-------------------------------

Lovely Error (Don't You Love Templates :)

Server.cc:435: error: no matching function for call to
`transform(__gnu_cxx::__n
ormal_iterator<Schedule::UserIFPrx*, std::vector std::alloc
ator >,
__gnu_cxx::__normal_iterator<Schedule::UserIFPrx*
, std::vector
Quote:
, std:
:back_insert_iterator<Schedule::UserISeq>, <unknown type>)'


------------------------------

Thanks!

-- Andrew Bell
ISU - Statistics - CSSM
[email]andrew.bell.ia (AT) gmail (DOT) com[/email]


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
David Abrahams
Guest





PostPosted: Thu Sep 22, 2005 9:01 am    Post subject: Re: transform - User Error or Compiler Error Reply with quote



[email]andrew.bell.ia (AT) gmail (DOT) com[/email] writes:

Quote:
Hi,

I get a compiler error when trying to use std::transform to
iteratate/modify a list. If I write the same code by hand, it compiles
fine. Am I misusing transform, or is this a compiler bug? The types
involved are complex templates, but I don't think this should have any
bearing on things.

-------------------------------

const UserIFSeq& uList;
UserIFSeq::const_iterator i;
UserISeq users;

//
// Compiles Fine.
//
for (i = uList.begin(); i != uList.end(); i++)
users.push_back(UserIPrx::uncheckedCast(*i));
//
// Produces Error.
//
std::transform(uList.begin(), uList.end(), back_inserter(users),
UserIPrx::uncheckedCast);
^

I think You're missing an ampersand right here (at the very least)

Quote:
-------------------------------

Lovely Error (Don't You Love Templates :)

Server.cc:435: error: no matching function for call to
`transform(__gnu_cxx::__n
ormal_iterator<Schedule::UserIFPrx*, std::vector std::alloc
ator >,
__gnu_cxx::__normal_iterator<Schedule::UserIFPrx*
, std::vector , std:
:back_insert_iterator ^^^^^^^^^^^^^^

Here's your clue. This is not a "template error message" problem.
It's just a stupid compiler problem. The compiler should tell you
that you can't pass a member function to a function; you can only pass
its address.

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Thomas Tutone
Guest





PostPosted: Thu Sep 22, 2005 9:12 am    Post subject: Re: transform - User Error or Compiler Error Reply with quote



andrew.bell wrote:

Quote:
I get a compiler error when trying to use std::transform to
iteratate/modify a list. If I write the same code by hand, it compiles
fine. Am I misusing transform, or is this a compiler bug?

Hard to tell, because you don't provide a definition of
uncheckedCast(), but my guess is the former.

Quote:
const UserIFSeq& uList;
UserIFSeq::const_iterator i;
UserISeq users;

//
// Compiles Fine.
//
for (i = uList.begin(); i != uList.end(); i++)
users.push_back(UserIPrx::uncheckedCast(*i));
//
// Produces Error.
//
std::transform(uList.begin(), uList.end(), back_inserter(users),
UserIPrx::uncheckedCast);

Is uncheckedCast a function template? If so, you need to provide the
specialization - std::transform can't deduce the argument type - i.e.,
you need something like std::transform(from.begin(), from.end(),
std::back_inserter(dest), uncheckedCast<int>);

Or maybe you could use UserIFSSeq::value_type to generalize the
specialization.

Quote:

-------------------------------

Lovely Error (Don't You Love Templates Smile

Actually it's telling you that there's a problem with the last argument
to transform - i.e., the functor.

Quote:

Server.cc:435: error: no matching function for call to
`transform(__gnu_cxx::__n
ormal_iterator<Schedule::UserIFPrx*, std::vector std::alloc
ator >,
__gnu_cxx::__normal_iterator<Schedule::UserIFPrx*
, std::vector , std:
:back_insert_iterator

*** Here's the last argument to transform:

Quote:
unknown type>)'

That's your hint - the functor argument is causing it problems - the
compiler can't figure out what the functor is. Thus my guess that you
used a function template instead of a function.

Best regards,

Tom


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Maciej Sobczak
Guest





PostPosted: Thu Sep 22, 2005 9:19 am    Post subject: Re: transform - User Error or Compiler Error Reply with quote

[email]andrew.bell.ia (AT) gmail (DOT) com[/email] wrote:

Quote:
I get a compiler error when trying to use std::transform to
iteratate/modify a list.


Quote:
std::transform(uList.begin(), uList.end(), back_inserter(users),
UserIPrx::uncheckedCast);
^


Try &UserIPrx::uncheckedCast.
^


--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze
Guest





PostPosted: Thu Sep 22, 2005 9:20 am    Post subject: Re: transform - User Error or Compiler Error Reply with quote

[email]andrew.bell.ia (AT) gmail (DOT) com[/email] wrote:

Quote:
I get a compiler error when trying to use std::transform to
iteratate/modify a list. If I write the same code by hand, it
compiles fine. Am I misusing transform, or is this a compiler
bug? The types involved are complex templates, but I don't
think this should have any bearing on things.

-------------------------------

const UserIFSeq& uList;
UserIFSeq::const_iterator i;
UserISeq users;

//
// Compiles Fine.
//
for (i = uList.begin(); i != uList.end(); i++)
users.push_back(UserIPrx::uncheckedCast(*i));
//
// Produces Error.
//
std::transform(uList.begin(), uList.end(), back_inserter(users),
UserIPrx::uncheckedCast);

-------------------------------

You don't show the declaration for UserIPrx::uncheckedCast, but
I'll bet it's a template. In the explicit loop, you call it
with a parameter -- the compiler uses the parameter to
instantiate a specific function, which it then calls. In the
call to transform, you take its address. The compiler needs to
know the target pointer type in order to instantiate the
template. The problem is that the target is a template
parameter as well, and the compiler needs to know the type of
the argument in order to do type deduction on it.

You have a chicken and egg problem: in order to know how to
instantiate uncheckedCast, it must know the type of the
parameter which the instantiation is being used to initialize,
and in order to know the type of the parameter, it must know the
type of the argument being passed to it.

Quote:
Lovely Error (Don't You Love Templates :)

Server.cc:435: error: no matching function for call to
`transform(__gnu_cxx::__n
ormal_iterator<Schedule::UserIFPrx*, std::vector std::alloc
ator >,
__gnu_cxx::__normal_iterator<Schedule::UserIFPrx*
, std::vector , std:
:back_insert_iterator ^^^^^^^^^^^^^^


That's actually a pretty good clue. The *last* argument to
transform is of <unknown type>.

It's much more challenging when the unknown type is somewhere in
the middle:-).

Quote:
------------------------------

I'm not sure what the best solution here is. In order to use
std::transform directly, you have to specify explicitly the
instantiation of uncheckedCast that you want, something like:

(UserISeq::value_type (*)( UserIFSeq::value_type ))
&UserIPrx::uncheckedCast

For a single argument, that's a bit awkward.

Another solution which I've occasionally used has been to use an
inline forwarding function, the forwarding function isn't a
template, so the compiler has the information to instantiate
unckeckedCast at the call site, and std::transform in the
function. In this case, however, it seems even more awkward
that the above. Perhaps the simplest solution is a small
forwarding functional object, e.g.:

struct MyOp
{
UserISeq::value_type operator()(
UserIFSeq::value_type const in ) const
{
return UserIPrx::uncheckedCast( in ) ;
}
} ;

Then, call std::transform with MyOp() as the last argument.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
andrew.bell.ia@gmail.com
Guest





PostPosted: Thu Sep 22, 2005 7:06 pm    Post subject: Re: transform - User Error or Compiler Error Reply with quote

Thanks for the replys. I understand that the type of the last argument
to transform (a template) can't be deduced since it doesn't have it's
argument specified. I think the necessary typing is more cumbersome
than writing my own loop, so I think I'll stick with that.

A couple of people noted that I should have had a '&' before the name
of the static template function. Isn't the use of the name of the
function in this context interpreted as an address without adding the
'&'?

Thanks for any clarification.

-- Andrew Bell
[email]andrew.bell.ia (AT) gmail (DOT) com[/email]


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
David Bertoni
Guest





PostPosted: Fri Sep 23, 2005 5:44 pm    Post subject: Re: transform - User Error or Compiler Error Reply with quote

[email]andrew.bell.ia (AT) gmail (DOT) com[/email] wrote:
Quote:
Thanks for the replys. I understand that the type of the last argument
to transform (a template) can't be deduced since it doesn't have it's
argument specified. I think the necessary typing is more cumbersome
than writing my own loop, so I think I'll stick with that.

A couple of people noted that I should have had a '&' before the name
of the static template function. Isn't the use of the name of the
function in this context interpreted as an address without adding the
'&'?


Not for a pointer-to-member, which always require the ampersand, and the
qualified name (called a qualified-id in the standard).

See section 5.3.1.3 of the standard.

Dave

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze
Guest





PostPosted: Fri Sep 23, 2005 10:27 pm    Post subject: Re: transform - User Error or Compiler Error Reply with quote

[email]andrew.bell.ia (AT) gmail (DOT) com[/email] wrote:
Quote:
Thanks for the replys. I understand that the type of the last
argument to transform (a template) can't be deduced since it
doesn't have it's argument specified. I think the necessary
typing is more cumbersome than writing my own loop, so I think
I'll stick with that.

I may be easier to write your own loop, but is it easier to
read. When I see std::transform, I know what it does. When I
see a loop, I have to analyse it to find out.

Quote:
A couple of people noted that I should have had a '&' before
the name of the static template function. Isn't the use of
the name of the function in this context interpreted as an
address without adding the '&'?

No. A member function, static or not, requires the &. (But was
it a member function? Because there was no error because of the
missing &, I just supposed that UserIPrx was a namespace.)

Having said that, even with the missing &, I can't see the
compiler complaining about it until it knew that a pointer, and
not the function, was needed. And of course, it can only know
that once it has instantiated std::transform, and to instantiate
transform, it needs to know the type... And we're back to the
real problem.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze
Guest





PostPosted: Fri Sep 23, 2005 10:28 pm    Post subject: Re: transform - User Error or Compiler Error Reply with quote

David Abrahams wrote:
Quote:
andrew.bell.ia (AT) gmail (DOT) com writes:

I get a compiler error when trying to use std::transform to
iteratate/modify a list. If I write the same code by hand,
it compiles fine. Am I misusing transform, or is this a
compiler bug? The types involved are complex templates, but
I don't think this should have any bearing on things.

-------------------------------

const UserIFSeq& uList;
UserIFSeq::const_iterator i;
UserISeq users;

//
// Compiles Fine.
//
for (i = uList.begin(); i != uList.end(); i++)
users.push_back(UserIPrx::uncheckedCast(*i));
//
// Produces Error.
//
std::transform(uList.begin(), uList.end(), back_inserter(users),
UserIPrx::uncheckedCast);
^
I think You're missing an ampersand right here (at the very least)

More likely, UserIPrx is a namespace, so the ampersand isn't
needed. Of course, UserIPrx::uncheckedCast could also be a
functional object. On the other hand, from the error message...

Quote:
-------------------------------

Lovely Error (Don't You Love Templates :)

Server.cc:435: error: no matching function for call to
`transform(__gnu_cxx::__n
ormal_iterator<Schedule::UserIFPrx*, std::vector std::alloc
ator >,
__gnu_cxx::__normal_iterator<Schedule::UserIFPrx*
, std::vector , std:
:back_insert_iterator ^^^^^^^^^^^^^^

Here's your clue. This is not a "template error message" problem.

Well, it's not a problem if you know the compiler:-) (and the
namespace on the first parameter gives that away). This is the
standard text used by g++ when it can't do type deduction --
uses an unknown type, then says that it can't find a function
which takes an unknown type. And about the only reason type
deduction couldn't be done in this case would be that
UserIPrs::uncheckedCast is an overloaded function or a template
function (or both).

Quote:
It's just a stupid compiler problem. The compiler should tell
you that you can't pass a member function to a function; you
can only pass its address.

If uncheckedCast is a template, the compiler cannot tell you
anything about it until it has instantiated it. To instantiate
it, it needs to know the type.

If UserIPrx is a class, rather than a namespace, the compiler
could complain about the missing & first. From what little I
know about conpiler construction, however, that would be a
somewhat backwards way of doing it internally.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.