473,554 Members | 3,075 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 3239
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
31861
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
6008
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
1812
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++...
11
3338
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
2104
by: asbisht | last post by:
Greetings to everyone. #include<iostream> main( void ) { unsigned char value = 'A'; std::cout << tolower(value); return 0; }
2
1826
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
2976
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...
0
7584
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...
0
7783
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7546
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...
0
7876
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...
0
6128
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...
0
5143
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...
0
3535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2007
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
0
827
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...

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.