Can the types cname and name.h be different?

Is this code compliant?

#include <stdio.h>
#include <cstdio>

int main() {
    FILE *f1 = 0;
    std::FILE *f2 = f1;
}

Explanation: The standard states [headers]:

[...] the contents of each heading cnameshould be the same as the contents of the corresponding heading name.h[...], as if it were included. However, in the C ++ standard library, declarations are [...] located in the namespace (3.3.6) of the namespace std. It is not known whether these names will be first declared in the global namespace area and then entered into the namespace stdusing explicit use-declarations (7.3.3).

So if they are not introduced explicitly using -declarations, can they be of a different type? I do not think that the phrase “as if an inclusion” is final, since the other half of the text clearly contradicts this requirement, requiring that the names are in the namespace std.

+5
source share
3 answers

I do not believe that the paragraph says that they should be the same. This is just a revision of the original (C ++ 98) paragraph, which states:

C, name.h, , , cname, std - (7.3.3)

, C . , ++ 11 , . , - , C, std.

, , :

T Standard C ::T std::T , ::T std::T. ([extern.types], 17.6.4.3.4)

+2

, : FILE* stdio.h, std::FILE* cstdio, - .

(, , , <cstdio>, FILE* .)


. , , using. , , - , . - D.5 (2):

C, name.h, , , cname, . , (3.3.6) std - (7.3.3).

, , :

"C ":

// foo.h

struct Gizmo { /* ... */ };

// cfoo

#include "foo.h"
namespace std { using ::Gizmo; }


"++ C-:

// cfoo

namespace std 
{
    struct Gizmo { /* ... */ };
}

// foo.h

#include <cfoo>
using std::Gizmo;
+3

Yes, they can be of different types. Use C ++ types; C headers are available for compatibility only.

Consider if, as a comment on the above answer, the C ++ header was implemented as namespace std { #include "stdio.h" }; then ::FILEthey std::FILEwill represent different types.

+1
source

All Articles