transform algorithm and casting of functions

S

suresh

Hi

I saw this code in the net for converting case.

string s = "Amma";
transform(s.begin(),s.end(),s.begin(),(int(*)(int))toupper);

Q: what is the meaning of (int(*)(int))toupper ?

The code works fine when (int(*)(int))toupper is replaced
by ::toupper. But the code does not even compile, when i write just
std::toupper i.e.
transform(s.begin(),s.end(),s.begin(),std::toupper); //not even
compiling

Could you please explain the difference between the three?

thanks
suresh
 
A

Abhishek Padmanabh

Hi

I saw this code in the net for converting case.

string s = "Amma";
transform(s.begin(),s.end(),s.begin(),(int(*)(int))toupper);

Q: what is the meaning of  (int(*)(int))toupper ?

It means it is casting pointer to function toupper as a pointer to a
function having return type as int and takes one int argument. I think
that is not required as toupper is of the same function pointer type.
The code works fine when (int(*)(int))toupper is replaced
by ::toupper. But the code does not even compile, when i write just
std::toupper i.e.
transform(s.begin(),s.end(),s.begin(),std::toupper); //not even
compiling

Could you please explain the difference between the three?

I think you are using VC++ 6.0 (or something as old/non-conforming as
that). It does not put the C APIs into std namespace (they are in
global namespace). Get rid of it and std::toupper works.
 
S

suresh

Thanks abhishek for the comments. But I am using g++ on a debian etch
machine. It is not VC++ and std::toupper is not compiling...

any suggestions?

suresh
 

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

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top