473,799 Members | 3,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with <algorithm> transform

Working on a Kubuntu 64bit system "c++ (GCC) 4.0.3".

The following simple program extracted from p.497 & 499 of N.M.Josurris'
"The C++ Standard Library ... " (file t.cpp):

1 #include <string>
2 #include <iostream>
3 #include <algorithm>
4 #include <cctype>
5 using namespace std;
6 int main() {
7 string s("This is the zip code of Hodna 1223");
8 cout << "original: " << s <<endl;
9 transform(s.beg in(), s.end(),s.begin (), toupper);
10 cout << "upper: " << s << endl;
11 }

results in the following error output when executing 'c++ t.cpp'

t.cpp: In function ‘int main()’:
t.cpp:9: error: no matching function for call
to ‘transform(__ gnu_cxx::__norm al_iterator<cha r*, std::basic_stri ng<char,
std::char_trait s<char>, std::allocator< char >,
__gnu_cxx::__no rmal_iterator<c har*, std::basic_stri ng<char,
std::char_trait s<char>, std::allocator< char >,
__gnu_cxx::__no rmal_iterator<c har*, std::basic_stri ng<char,
std::char_trait s<char>, std::allocator< char >, <unknown type>)’

Something seems seriously wrong but I can't figure it.

Help, suggestions greatly appreciated.
Jan 6 '08 #1
11 2994
transform(s.beg in(), s.end(),s.begin (), toupper);
Something seems seriously wrong but I can't figure it.
Alas, toupper is a macro so you can't pass it as an argument.
Jan 6 '08 #2
On 2008-01-06 21:45, Andrew Koenig wrote:
> transform(s.beg in(), s.end(),s.begin (), toupper);
>Something seems seriously wrong but I can't figure it.

Alas, toupper is a macro so you can't pass it as an argument.
Are you sure? In C99 it is a function specified as int toupper(int c)
(section 7.4.2.2, "The toupper function") and in C++98 table 45 it is
also listed as a function.

To the OP: the code you posted compiled and ran fine with MSVC++ so I am
not sure why it did not work for you. However there is also a toupper()
function in C++ declared in the <localeheader which might work (i.e.
replace <cctypewith <localeand try again).

--
Erik Wikström
Jan 6 '08 #3
In article <lfagj.804$xA6. 524@trndny09>, ge************@ verizon.mail
says...
Working on a Kubuntu 64bit system "c++ (GCC) 4.0.3".

The following simple program extracted from p.497 & 499 of N.M.Josurris'
"The C++ Standard Library ... " (file t.cpp):
[ ... code elided ]
results in the following error output when executing 'c++ t.cpp'
The problem appears to be with your installation of gcc -- the code is
fine.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 6 '08 #4
In article <U5************ ********@bgtnsc 05-news.ops.worldn et.att.net>,
ar*@acm.org says...
transform(s.beg in(), s.end(),s.begin (), toupper);
Something seems seriously wrong but I can't figure it.

Alas, toupper is a macro so you can't pass it as an argument.
Unless my memory is worse than usual today, a function-like macro is
only supposed to be expanded when/if its name is followed by an open
parenthesis, so in this case the fact that there may be a macro by that
name shouldn't make any difference.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 6 '08 #5
On Jan 6, 9:45 pm, "Andrew Koenig" <a...@acm.orgwr ote:
transform(s.beg in(), s.end(),s.begin (), toupper);
Something seems seriously wrong but I can't figure it.
Alas, toupper is a macro so you can't pass it as an argument.
Comme now, Andy, you know better than that. In C, toupper may
be a function style macro, but the function declaration must
also be present (hidden by the macro), and will be used if the
token immediately following the symbol isn't a '('. In C++,
toupper is a set of overloaded functions (including a function
template), and can't be used without some sort of overload
disambiguation. Typically, this will be the arguments in a
function call or, when taking the address, the type of the
destination. The problem here is that where the code takes the
address matches a templated parameter of a template function.
Which means that the compiler can't possibly do type deduction,
and the call fails.

Of course, the original code couldn't work anyway; it expects to
call toupper with a single, char argument, and the only overload
of toupper that can be legally called with a char argument
requires a locale as the second argument.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 7 '08 #6
On Jan 6, 10:17 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2008-01-06 21:45, Andrew Koenig wrote:
transform(s.beg in(), s.end(),s.begin (), toupper);
Something seems seriously wrong but I can't figure it.
Alas, toupper is a macro so you can't pass it as an argument.
Are you sure? In C99 it is a function specified as int toupper(int c)
(section 7.4.2.2, "The toupper function") and in C++98 table 45 it is
also listed as a function.
To the OP: the code you posted compiled and ran fine with
MSVC++ so I am not sure why it did not work for you.
It's undefined behavior, so it might compile. I'd be very
surprised if it worked correctly with VC++, however (unless you
compiled with the /J option).
However there is also a toupper() function in C++ declared in
the <localeheader which might work (i.e. replace <cctype>
with <localeand try again).
There are two separate issues involved here. The first is that
in C++, toupper is overloaded, with one of the overloads (the
one in <localebeing a function template. And that the
standard allows any standard C++ header to include any other.
Obviously, if the template function is visible, the compiler
can't possibly resolve type deduction for std::transform unless
you somehow tell it which overload you want to use: it's using
the type of the argument (i.e. the name of the function) to
deduce the template type of std::transfor, and it needs to know
the target type to resolve the overload on the name. If
<localehasn't been included (directly or indirectly), on the
other hand, the only function visible is the one in <cctype>,
type deduction works, and the code compiles.

The second problem is that if char is signed (which it is by
default in VC++), there is no overload of std::toupper which can
be legally called with just a single char argument: the function
template in <localerequir es two arguments, and the function in
<cctypetakes an int in the range [0...UCHAR_MAX] as an
argument---if char is signed, it can (and often will) have a
negative value, which results in undefined behavior if it is
passed to the toupper function in <ctype>. This error is so
common that a number of implementations today actually make the
code work anyway. For all values except EOF (which is almost
always -1). (If you're using ISO 8859-1---one of the more
widespread single byte encoding---then -1 from a char would
correspond to a 'ÿ', a latin small letter y with diaeresis. And
ISO 8859-1 doesn't contain a latin capital letter y with
diaeresis, so returning the same value is correct.)

Finally, of course, you might even have to ask if any of the
standard toupper functions are applicable. There is not,
generally, a one to one mapping of lower to upper, and some
lower case characters might map to a two character sequence in
upper case (German 'ß' becomes "SS", or in some special
contexts, "SZ"; Swiss German 'ä' becomes either "Ae" or "AE",
etc.). And of course, toupper (in all its forms) is totally
useless if you have a multibyte encoding, like UTF-8 (which is
the default, I believe, in most modern Linux distributions).
Depending on the application, such issues may or may not be
relevant.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 7 '08 #7
On Jan 6, 11:15 pm, "Alf P. Steinbach" <al...@start.no wrote:
* Erik Wikström:
On 2008-01-06 21:45, Andrew Koenig wrote:
> transform(s.beg in(), s.end(),s.begin (), toupper);
Something seems seriously wrong but I can't figure it.
Alas, toupper is a macro so you can't pass it as an argument.
Are you sure? In C99 it is a function specified as int
toupper(int c) (section 7.4.2.2, "The toupper function") and
in C++98 table 45 it is also listed as a function.
The reference you give is correct, and means that Andrew
Koenig made a mistake.
Surprisingly.
To the OP: the code you posted compiled and ran fine with
MSVC++ so I am not sure why it did not work for you.
Ironically, the error seems to be due to Koenig lookup... :-)
Not really, but see my other postings.
However, adding an include of <localedoes not reproduce the
error with MSVC 7.1.
Are you kidding? (I just tried it with VC++ 8, and I get the
same results. Could it be that Dinkumware uses some form of
concept checking to exclude functions here which can't be called
with only one argument? Or?)

I haven't tried it, but he did have a "using namespace std;" in
there, so everything in <localeshould be visible. In theory
at least---and probably in practice, including <ctype.h(*NOT *
<cctype>), and specifying ::toupper (to block any chance of
finding the functions in <locale>) should allow the code to
compile.
As Newton puportedly said, I frame no hypothesis.
However there is also a toupper() function in C++ declared
in the <localeheader which might work (i.e. replace
<cctypewith <localeand try again).
No, that one is a bit different: it suffers from the usual
standard library (except STL parts) unusability and
complexity, taking a locale argument as second argument.
Which is not a problem for occasional use.

What you really need is some sort of functional object, however,
which will get the ctype facet once from the locale, and use the
toupper of the facet.
A cure for the immediate problem is to write
::toupper
but this may be a compiler-specific cure (I'm not sure).
If he includes <ctype.h>, the resulting code is guaranteed to
compile.

[...]
So to sum up, the Josuttis book example may be correct for the
given data (only ASCII characters), but not for an arbitrary
input string, and, g++ apparently does something funny.
One of the C++ headers he's included probably includes <locale>
in the g++ implementation. The standard clearly says that this
is unspecified (even though it can cause no number of
portability problems).

Anyway, I'd be very interested in hearing from someone familiar
with the VC++ implementation, explaining why the original code
still compiles when you include <locale>. Like you, I can't
explain it.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 7 '08 #8
On Jan 6, 11:08 pm, Jerry Coffin <jcof...@taeus. comwrote:
In article <lfagj.804$xA6. 524@trndny09>, gerald.even...@ verizon.mail
says...
Working on a Kubuntu 64bit system "c++ (GCC) 4.0.3".
The following simple program extracted from p.497 & 499 of N.M.Josurris'
"The C++ Standard Library ... " (file t.cpp):
[ ... code elided ]
results in the following error output when executing 'c++ t.cpp'
The problem appears to be with your installation of gcc -- the
code is fine.
No it's not. Whether is will compiler or not is unspecified:
any C++ header may include any other, and if <localeis
included, you should have a serious problem with template type
deduction for the call to std::transform. (From experience, g++
headers tend to include the world; when you develope under g++,
then port to other compilers, you very quickly get used to
having to add includes for headers you'd forgotten.)

Of course, even if it compiles, it has undefined behavior if
char is signed.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 7 '08 #9
Digging through Prada's "C++ Primer Plus" (5th) I found notes on
p.936-7 relating to C library functions delared as having int returns
and problems thereof, so I added a line to t.cpp and changed line
13's function name:

1 #include <string>
2 #include <iostream>
3 #include <algorithm>
4 #include <cctype>
5 using namespace std;
6 /* I added the following line because cctype
7 * apparently declares 'int toupper(char)'
8 */
9 static char toUpper(char c) { return toupper(c); }
10 int main() {
11 string s("This is the zip code of Hodna 1223");
12 cout << "original: " << s <<endl;
13 transform(s.beg in(), s.end(),s.begin (), toUpper);
14 cout << "upper: " << s << endl;
15 }

then:
gie@charon:~/Letters/src$ g++ t.cpp
gie@charon:~/Letters/src$ ./a.out
original: This is the zip code of Hodna 1223
upper: THIS IS THE ZIP CODE OF HODNA 1223
gie@charon:~/Letters/src$
Problem solved.
Thanks for the help. This looks like a pop quiz but I did not
mean it that way.
Jan 7 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
3854
by: Steffen Brinkmann | last post by:
Hi! I tried to modify the transform algorithm in a way that it doesn't take iterators, but a reference to a container class and a value, because Mostly I need to do an operation of a container and a single number (e.g. multiply all the values in a vector by 3 or so). So, this is my intent: #ifndef ALGORITHM_EXT_HH #define ALGORITHM_EXT_HH
5
4324
by: google | last post by:
Hi All, I'm just getting started learning to use <algorithm> instead of loads of little for loops, and I'm looking for a bit of advice/mentoring re: implementing the following... I have a vector of boost::shared_ptr's, some of which may be NULL. I'd like to find out if there are any non-NULL elements in the vector. Here's what I have at the moment.
7
2609
by: Wei | last post by:
Hi all, I found out I can use the max function which is defined in STL <algorithmwithout including this header in my program. The compilers I used are GNU g++ 3.4.4 and Visual Studio C++ 2005. The program is as follows: #include <iostream> //#include <algorithm <-- I don't have to include <algorithmin
10
6078
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to remove the elements with odd values from your list * and the even values from your vector.
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10252
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10231
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9073
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.