help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: tolower - std namespace?


From: Thomas Maeder
Subject: Re: tolower - std namespace?
Date: Sat, 15 Mar 2008 10:12:58 +0100
User-agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux)

sanjeev.sapkota@gmail.com writes:

> I've read multiple posts about how to get the 'tolower' function to
> work in gcc. From what I understand, the problem is that there are two
> versions - you must cast to specify which version you want to use.
>
>   transform(tempString.begin(),tempString.end(),tempString.begin(),
>             (int(*)(int))std::tolower);

Thanks for following my request to change into a g++ group.

Here's my "threatened" information on why what you are attempting to
do probably isn't a good idea:

You are attempting to invoke the 1 argument verison of
std::tolower(). That's the version C++ inherits from C. As most
<cctype> functions, std::tolower() expects its argument to either have
the value -1 or a value in the range representable by unsigned chars,
i.e. 0..UCHAR_MAX; otherwise, the program has undefined behavior.

If your char type is signed, and tempString contains characters with
negative codes, the requirement imposed by std::tolower() will not be
met in the above call to std::transform() (resp. a working version
thereof).

If your program uses an ASCII based character encoding, such as an
ISO-8859 (or "ANSI") encoding, everyday characters such as äöüéàéñ do
have negative codes.


> This works with gcc, but not with MS Visual Studio 2003. It only works
> with MS Visual Studio 2003 if you remove the std::.

"works" and "doesn't work" are very vague. What error messsage do you
get?


> I'm not quite sure why this is, but I read that there are two versions
> of tolower. One is the global namespace, and comes from ctype.h, and
> the other one is in the std namespace.

Unfortunately, the situation is more complicated:
- <cctype> declares the 1 argument version of std::tolower()
- <locale> declares the 2 argument version of std::tolower()
- in C++, <ctype.h> works like <cctype> but adds a using declaration
  for std::tolower in the global namespace

And in reality, C++ implemetations often don't implement the 3rd item
correctly. So the best thing IMHO is to not #include <ctype.h> in new
code and rely on the tolower() overloads in namespace std.


> This link (http://www.thescripts.com/forum/thread161158.html) states
> that std::tolower takes two arguments, not one. This link
> (http://www.cplusplus.com/ reference/clibrary/cctype/tolower.html)
> shows that the tolower in cctype.h takes a single argument. I am
> also aware that transform must take a binary argument.

There is no such thing as <cctype.h> in C++. It's either <cctype> or
<ctype.h>. Both declare a 1 argument version of std::tolower().


> So, if the problem *is* because the std::tolower version takes 2
> arguments (and not one), this lead me to ask why it works in gcc?

Please post a minimal, complete program that demonstrates your
problem. Without seeing what headers you #include, your audience can
only make guesses.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]