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: Wed, 19 Mar 2008 17:52:53 +0100
User-agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux)

Taras_96 <taras.di@gmail.com> writes:

> I tried a number of things with some test code. The test code is:
>
> #include <iostream>
> #include <string>
> #include <algorithm>
> #include <vector>
>
> int main()
> {
>   std::string tempString = "BLAH";
>       transform(tempString.begin(),tempString.end(),tempString.begin(),
>                                               (int(*)(int))std::tolower);
>   std::cout << tempString << std::endl;
>  }
>
> I will alter the above code by:
> 1) Including <cctype>, then changing this to include <ctype.h>
> 2) Switching between std::tolower and tolower
>
> 1. As is:
> --------------------
> g++ compiled fine
>
> MSV2008:tolower.cpp(10) : error C2039: 'tolower' : is not a member of
> 'std'
>
> Where is 'tolower' being pulled in from for g++? I'm not including
> cctype or ctype.h...

The ISO C++ Standard allows Standard Library headers to freely
#include other Standard Library headers. It seems that one of the
headers your code in turn #includes <cctype>, possibly indirectly. If
I were interested, I'd have a look at <iostram> and <string> first.

The behavior of both compilers is thus correct.


> 2. #include <cctype>, calling std::tolower
> --------------------
> g++ compiled fine
> MSV2008 works fine

Good.


> 2. #include <cctype>, calling tolower
> --------------------
> g++ compiled fine
> MSV2008 works fine

Not good. 


> 3. #include <ctype.h>, calling std::tolower
> --------------------
> g++ compiled fine
> MSV2008: tolower.cpp(11) : error C2039: 'tolower' : is not a member of
> 'std'
>
> It seems that including ctype.h is placing to lower into the global
> namespace, but also hiding it from the std:: one?

I wouldn't call it hiding; the function is probably just not declared
in namespace std.

The 1998 Standard states (ยง D.5):

Each C header, whose name has the form name.h, behaves as if each name
placed in the Standard library namespace by the corresponding cname
header is also placed withing the namespace scope of the namespace std
and is followed by an explicit using-declaration.

[Example: The header <cstdlib> provides its declarations and
definitions within the namespace std. The header <stdlib.h> makes these
available in the global name space, much as in the C Standard.]

It seems that Microsoft's <ctype.h> only fulfills the second part,
ignoring the part before "also".


That's an instance of what I meant when I wrote:

>> And in reality, C++ implemetations often don't implement the 3rd
>> item correctly.

in my previous post.


> 4. #include <ctype.h>, calling tolower
> --------------------
> g++ compiled fine
> MSV2008: works fine

Good. That's the C compatibility mode.


> What are the reasons for these differences?

See above. Partly the implementation's freedom of choice, and partly
implementation bugs.


Summing things up, you should be fine using option 2. or 4. I'd use
2. in new code; when using old C code, 4. comes to our rescue.


reply via email to

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