473,890 Members | 3,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compiles with tolower from global namespace but not with std::tolower

Hello, consider this program:
#include <iostream>
#include <algorithm>
#include <string>
#include <cctype>

int
main()
{
std::string s = "HEJ";
std::transform (s.begin(), s.end(), s.begin(), std::tolower);
std::cout << "s: " << s << std::endl;
}

It doesn't compile, I get:
$ make
g++ -Wall -W -ansi -pedantic -g -c -o issue.o issue.cpp
issue.cpp: In function `int main()':
issue.cpp:10: error: no matching function for call to
`transform(__gn u_cxx::__normal _iterator<char* , 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>)'
make: *** [issue.o] Error 1
¨
*but*, if I change the call to std::transform to
std::transform (s.begin(), s.end(), s.begin(), ::tolower);
(i.e., I pass ::tolower instead of std::tolower)

it compiles cleanly and produces the expected output when run. So it
works with the tolower that lives in the global namespace, why?

Is this the correct behaviour? Someone said that the first version
compiles cleanly on visual studio (unknown version) but I haven't had
the chance to try myself.

/ Eric

Sep 2 '05 #1
4 3256
int
main()
{
std::string s = "HEJ";
std::transform (s.begin(), s.end(), s.begin(), std::tolower);
std::cout << "s: " << s << std::endl;
}

std::tolower takes 2 arguments, not one. The second is locale. You can use
bind2nd to adapt it.

cheers,
Marcin
Sep 2 '05 #2
Eric Lilja wrote:
Hello, consider this program:
#include <iostream>
#include <algorithm>
#include <string>
#include <cctype>

int
main()
{
std::string s = "HEJ";
std::transform (s.begin(), s.end(), s.begin(), std::tolower);
std::cout << "s: " << s << std::endl;
}

It doesn't compile, I get:
[error message snipped]
$ make
*but*, if I change the call to std::transform to
std::transform (s.begin(), s.end(), s.begin(), ::tolower);
(i.e., I pass ::tolower instead of std::tolower)

it compiles cleanly and produces the expected output when run. So it
works with the tolower that lives in the global namespace, why?

Is this the correct behaviour?


This comes up so often it really should be in the FAQ. Search for
transform and tolower in google groups and you'll see the answer (many,
many times):

http://groups.google.com/groups?q=transform+tolower

Best regards,

Tom

Sep 2 '05 #3
Thomas Tutone wrote:
Eric Lilja wrote:

Hello, consider this program:
#include <iostream>
#include <algorithm>
#include <string>
#include <cctype>

int
main()
{
std::string s = "HEJ";
std::transform (s.begin(), s.end(), s.begin(), std::tolower);
std::cout << "s: " << s << std::endl;
}

It doesn't compile, I get:

[error message snipped]

$ make
*but*, if I change the call to std::transform to
std::transfor m (s.begin(), s.end(), s.begin(), ::tolower);
(i.e., I pass ::tolower instead of std::tolower)

it compiles cleanly and produces the expected output when run. So it
works with the tolower that lives in the global namespace, why?

Is this the correct behaviour?

This comes up so often it really should be in the FAQ. Search for
transform and tolower in google groups and you'll see the answer (many,
many times):

http://groups.google.com/groups?q=transform+tolower

Best regards,

Tom


It should also be in the FAQ that to use ::tolower in this form is to
risk undefined behaviour. It is undefined behaviour if ::tolower is
passed a negative value (excepting the value of EOF). But since char
*may* be signed you are rsking that undefined behaviour. The correct
form is something like this

inline int safe_tolower(ch ar ch)
{
return ::tolower((unsi gned char)ch);
}

std::transform (s.begin(), s.end(), s.begin(), safe_tolower);

John
Sep 2 '05 #4
> std::tolower takes 2 arguments, not one. The second is locale. You can
use bind2nd to adapt it.

Correct me if I'm wrong but I don't think that you can use bind2nd whith
std::tolower since the locale is passed by reference and not by value.

You may want to define your own functor:

struct ToLower
{
char operator()(char c)
{
return std::tolower(c, loc_);
}
private:
std::locale loc_; // default locale of the user environment
};

and use it like that:

std::transform( s.begin(), s.end(), s.begin(), ToLower());
--
Thierry Miceli
www.refpp.com
Sep 2 '05 #5

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

Similar topics

3
31877
by: qazmlp | last post by:
I was using the following code to convert the string to lowercase. string foo = "Some Mixed Case Text"; transform(foo.begin(), foo.end(), foo.begin(), tolower); I thought the above code is portable. But, the following page has a different view on this: http://lists.debian.org/debian-gcc/2002/debian-gcc-200204/msg00092.html
13
6040
by: David Rubin | last post by:
I get an error when I compile the following code: #include <algorithm> #include <cctype> #include <iostream> #include <string> using namespace std; string&
3
1823
by: Ahsan | last post by:
Hi All, I am having prolems with converting upper case characters to lower case in Unix's C. I saw the man pages and "tolower" function does it. But in my case its not working. My code is as follows: ============================================================ gets ( input ); sscanf(input, "%s", Cmd); for( p = 0; p < strlen( Cmd ); p++ )/* Changing all input to lower
11
3370
by: TheDD | last post by:
Hello, i don't manage to use the tolower() function: #include <algorithm> #include <iostream> #include <locale> #include <string> using std::cout;
9
2120
by: asbisht | last post by:
Greetings to everyone. #include<iostream> main( void ) { unsigned char value = 'A'; std::cout << tolower(value); return 0; }
2
1840
by: Daniel Aarno | last post by:
As far as I can tell the locale header should provide a tolower function however the following failes for me under gcc 3.4. transform(begin, end, begin2, std::tolower); with an error that the last arg is of unknown type. Any ideas on why?
13
3007
by: Soumen | last post by:
I wanted convert a mixed case string to a lower case one. And I tried following code: std::transform(mixedCaseString.begin(), mixedCaseString::end(), mixedCaseString.begin(), std::ptr_fun(tolower)); Even though I's including cctype and algorithm, I's getting compiler (g ++ 3.3.6) error: no matching function for call to `ptr_fun(<unknown type>)'
0
9979
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
10827
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
10924
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
10463
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9637
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
7170
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6049
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4681
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
4275
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.