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 4 3254
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
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
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
> 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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&
|
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
|
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;
|
by: asbisht |
last post by:
Greetings to everyone.
#include<iostream>
main( void )
{
unsigned char value = 'A';
std::cout << tolower(value);
return 0;
}
| |
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?
|
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>)'
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
|
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,...
|
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...
| |
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...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |