473,493 Members | 2,245 Online
Bytes | Software Development & Data Engineering Community
Create 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(__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<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 3233
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::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


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(char ch)
{
return ::tolower((unsigned 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
31854
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...
13
5993
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
1802
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...
11
3330
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
2090
by: asbisht | last post by:
Greetings to everyone. #include<iostream> main( void ) { unsigned char value = 'A'; std::cout << tolower(value); return 0; }
2
1825
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...
13
2971
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(),...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6873
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...
0
7367
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...
1
4889
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4579
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...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1400
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 ...
1
644
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
285
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...

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.