[cfe-dev] [libcxx/test] Puzzling floating point behaviour

Howard Hinnant hhinnant at apple.com
Sat Dec 10 14:17:18 PST 2011


On Dec 10, 2011, at 4:40 PM, Marc Glisse wrote:

> On Sat, 10 Dec 2011, Howard Hinnant wrote:
> 
>> On Dec 10, 2011, at 3:59 PM, Marc Glisse wrote:
>> 
>>> On Sat, 10 Dec 2011, Howard Hinnant wrote:
>>> 
>>>> On Dec 10, 2011, at 2:57 PM, Marc Glisse wrote:
>>>> 
>>>>> On Sat, 10 Dec 2011, Howard Hinnant wrote:
>>>>> 
>>>>>> In <math.h> you have:
>>>>>> 
>>>>>> double exp(double);
>>>>>> double sin(double);
>>>>>> double cos(double);
>>>>>> 
>>>>>> In <cmath> you *additionally* have:
>>>>>> 
>>>>>> namespace std
>>>>>> {
>>>>>> float exp(float);
>>>>>> double exp(double);
>>>>>> long double exp(long double);
>>>>>> 
>>>>>> float sin(float);
>>>>>> double sin(double);
>>>>>> long double sin(long double);
>>>>>> 
>>>>>> float cos(float);
>>>>>> double cos(double);
>>>>>> long double cos(long double);
>>>>>> }
>>>>> 
>>>>> We do agree that this is illegal, even in C++11, right?
>>>> 
>>>> No.
>>>> 
>>>> D.5  C standard library headers  [depr.c.headers]
>>>> 
>>>> 1 ...
>>>> 
>>>> 2 Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3).
>>>> 
>>>> 3 [ Example: The header <cstdlib> assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. The header <stdlib.h> assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespace std. — end example ]
>>> 
>>> As far as I can tell, that allows you to declare functions in the global
>>> namespace, but not to omit half of the declarations. It thought it was a
>>> well-known defect that the change made in C++11 still didn't make the
>>> plain C headers valid in C++, so I am a bit surprised at your reaction...
>> 
>> Is your complaint with <math.h> or <cmath>?
> 
> math.h, sorry for not being more precise.
> (I have not tried to interpret whether the wording allows cmath to leave 
> the global namespace with only half the overloads declared, but that's a 
> different problem)

I see.  Yes, I think you are correct.  However the gist of the issue that changed these words was that C++98/03 way over-stepped its bounds in trying to specify the contents of the name.h headers:

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#456

Therefore if this gets brought to the attention of the LWG (and I think it probably should), it's a pretty safe bet that the action taken will be to move more of the name.h specification off to the C standard.  Indeed my opinion is that the name.h headers should be removed from the C++ standard.  They would still be available to C++ programers.  But if you want to find out what they do, you have to read the C standard.

At the very least, anything you read in the C++ standard about the name.h headers, you should take with a grain of salt.  The C++ community should be thrilled we got the C prototypes wrapped in extern "C" in the name.h headers. ;-)

Howard





More information about the cfe-dev mailing list