Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: size_t ... standards

Reply
Thread Tools

Re: size_t ... standards

 
 
Howard Hinnant
Guest
Posts: n/a
 
      06-29-2003
In article <bdn2gu$987$1$(E-Mail Removed)>, <(E-Mail Removed)>
wrote:

| Im compiling a library off the net (GNU cgicc), and CodeWarrior has
| flagged up a few errors. They relate to size_t. Basically...
|
| is size_t C++ ok? and do you need to include <stddef.h> (like using C)
| to specify size_t, or is it different in C++? Just wondering if the
| library is right, or the compiler (I had to change size_type to size_t
| on a function and had to include <stddef.h> on a couple of files to get
| it functioning).

The C++ standard says that size_t's full names is std::size_t. On
platforms where we control the C lib, we follow the standard on this.
I'm guessing size_t was available, but simply in namespace std. Your
fix (#include <stddef.h>) is a good way to go. This not only makes
sure std::size_t is available, it also imports it into the global
namespace, so you can access it without the std:: prefix. And such a
fix is very portable.

--
Howard Hinnant
Metrowerks
 
Reply With Quote
 
 
 
 
Jim Fischer
Guest
Posts: n/a
 
      06-30-2003
Howard Hinnant wrote:
> In article <bdn2gu$987$1$(E-Mail Removed)>, <(E-Mail Removed)>
> wrote:
>
> | Im compiling a library off the net (GNU cgicc), and CodeWarrior has
> | flagged up a few errors. They relate to size_t. Basically...
> |
> | is size_t C++ ok? and do you need to include <stddef.h> (like using C)
> | to specify size_t, or is it different in C++? Just wondering if the
> | library is right, or the compiler (I had to change size_type to size_t
> | on a function and had to include <stddef.h> on a couple of files to get
> | it functioning).
>
> The C++ standard says that size_t's full names is std::size_t. On
> platforms where we control the C lib, we follow the standard on this.
> I'm guessing size_t was available, but simply in namespace std. Your
> fix (#include <stddef.h>) is a good way to go. This not only makes
> sure std::size_t is available, it also imports it into the global
> namespace, so you can access it without the std:: prefix. And such a
> fix is very portable.


I could be wrong on this, but my recollection is that names from the
standard C library (e.g., size_t) are only introduced into C++'s 'std'
namespace when the '<cname>' C++ headers are used. Furthermore, these
'<cname>' headers also employ using-declarations to explicitly introduce
the C library names (e.g., size_t) into the C++ app's global namespace.
For example,

#include <cstddef> // ISO C++ header

The C++ header 'cstddef' introduces the name 'size_t' (for example) into
C++'s 'std' namespace, and follows it with an explicit using-declaration
(D5/3), i.e.,

namespace std { typedef {whatever} size_t; }
using std::size_t;

Since the name 'size_t' is introduced into the global namespace by the
using-declaration, the C++ program can simply specify 'size_t' and not
'std::size_t'. IIRC, this holds for most (but not all) "things" in the C
library -- e.g., with <cstdio>, the C++ program can simply specify
'printf' and not 'std:rintf':

#include <cstdio>
int main() {
printf("Hello,World\n"); // OK
}


OTOH, C library names (e.g., 'size_t') are NOT introduced into the C++
'std' namespace when a C header is specified in a C++ translation unit:

#include <stddef.h> /* ISO C header */
// At this point, 'size_t' is NOT in the 'std' namespace


The bottom line here is this: the unqualified name 'size_t' should be
visible within the global namespace of a C++ program if either the C++
header <cstddef> or the C header <stddef.h> is specified.

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com


 
Reply With Quote
 
 
 
 
Howard Hinnant
Guest
Posts: n/a
 
      06-30-2003
In article <pnMLa.3624$(E-Mail Removed)2.webusenet.com>, Jim Fischer
<(E-Mail Removed)> wrote:

| Howard Hinnant wrote:
| > In article <bdn2gu$987$1$(E-Mail Removed)>, <(E-Mail Removed)>
| > wrote:
| >
| > The C++ standard says that size_t's full names is std::size_t. On
| > platforms where we control the C lib, we follow the standard on this.
| > I'm guessing size_t was available, but simply in namespace std. Your
| > fix (#include <stddef.h>) is a good way to go. This not only makes
| > sure std::size_t is available, it also imports it into the global
| > namespace, so you can access it without the std:: prefix. And such a
| > fix is very portable.
|
| I could be wrong on this, but my recollection is that names from the
| standard C library (e.g., size_t) are only introduced into C++'s 'std'
| namespace when the '<cname>' C++ headers are used. Furthermore, these
| '<cname>' headers also employ using-declarations to explicitly introduce
| the C library names (e.g., size_t) into the C++ app's global namespace.
| For example,
|
| #include <cstddef> // ISO C++ header
|
| The C++ header 'cstddef' introduces the name 'size_t' (for example) into
| C++'s 'std' namespace, and follows it with an explicit using-declaration
| (D5/3), i.e.,
|
| namespace std { typedef {whatever} size_t; }
| using std::size_t;
|
| Since the name 'size_t' is introduced into the global namespace by the
| using-declaration, the C++ program can simply specify 'size_t' and not
| 'std::size_t'. IIRC, this holds for most (but not all) "things" in the C
| library -- e.g., with <cstdio>, the C++ program can simply specify
| 'printf' and not 'std:rintf':
|
| #include <cstdio>
| int main() {
| printf("Hello,World\n"); // OK
| }
|
|
| OTOH, C library names (e.g., 'size_t') are NOT introduced into the C++
| 'std' namespace when a C header is specified in a C++ translation unit:
|
| #include <stddef.h> /* ISO C header */
| // At this point, 'size_t' is NOT in the 'std' namespace
|
|
| The bottom line here is this: the unqualified name 'size_t' should be
| visible within the global namespace of a C++ program if either the C++
| header <cstddef> or the C header <stddef.h> is specified.

I believe you have it backwards according to the standard. However
your interpretation is fairly close to common practice.

From the C++ standard 17.4.1.2:

| -4- Except as noted in clauses lib.language.support through
| lib.input.output , the contents of each header cname shall be the same
| as that of the corresponding header name .h , as specified in ISO/IEC
| 9899:1990 Programming Languages C (Clause 7), or ISO/IEC:1990
| Programming Languages --- C AMENDMENT 1: C Integrity, (Clause 7), as
| appropriate, as if by inclusion. In the C++ Standard Library, however,
| the declarations and definitions (except for names which are defined as
| macros in C) are within namespace scope ( basic.scope.namespace ) of
| the namespace std.

| -7- depr.c.headers , Standard C library headers, describes the effects
| of using the name .h (C header) form in a C++ program.*
| [Footnote: The ".h" headers dump all their names into the global
| namespace, whereas the newer forms keep their names in namespace std .
| Therefore, the newer forms are the preferred forms for all uses except
| for C++ programs which are intended to be strictly compatible with C.
| --- end foonote]

--
Howard Hinnant
Metrowerks
 
Reply With Quote
 
Jim Fischer
Guest
Posts: n/a
 
      06-30-2003
Howard Hinnant wrote:
> In article <pnMLa.3624$(E-Mail Removed)2.webusenet.com>, Jim Fischer
> <(E-Mail Removed)> wrote:
>
> | Howard Hinnant wrote:
> | > In article <bdn2gu$987$1$(E-Mail Removed)>, <(E-Mail Removed)>
> | > wrote:
> | >
> | > The C++ standard says that size_t's full names is std::size_t. On
> | > platforms where we control the C lib, we follow the standard on this.
> | > I'm guessing size_t was available, but simply in namespace std. Your
> | > fix (#include <stddef.h>) is a good way to go. This not only makes
> | > sure std::size_t is available, it also imports it into the global
> | > namespace, so you can access it without the std:: prefix. And such a
> | > fix is very portable.
> |
> | I could be wrong on this, but my recollection is that names from the
> | standard C library (e.g., size_t) are only introduced into C++'s 'std'
> | namespace when the '<cname>' C++ headers are used. Furthermore, these
> | '<cname>' headers also employ using-declarations to explicitly introduce
> | the C library names (e.g., size_t) into the C++ app's global namespace.
> | For example,
> |
> | #include <cstddef> // ISO C++ header
> |
> | The C++ header 'cstddef' introduces the name 'size_t' (for example) into
> | C++'s 'std' namespace, and follows it with an explicit using-declaration
> | (D5/3), i.e.,


[Correction: I quoted D.5/3 here, but I probably should have quoted
D.5/2 instead. Paragraph D.5/3 is an "Example" paragraph for D.5/2.]


> |
> | namespace std { typedef {whatever} size_t; }
> | using std::size_t;
> |
> | Since the name 'size_t' is introduced into the global namespace by the
> | using-declaration, the C++ program can simply specify 'size_t' and not
> | 'std::size_t'. IIRC, this holds for most (but not all) "things" in the C
> | library -- e.g., with <cstdio>, the C++ program can simply specify
> | 'printf' and not 'std:rintf':
> |
> | #include <cstdio>
> | int main() {
> | printf("Hello,World\n"); // OK
> | }
> |
> |
> | OTOH, C library names (e.g., 'size_t') are NOT introduced into the C++
> | 'std' namespace when a C header is specified in a C++ translation unit:
> |
> | #include <stddef.h> /* ISO C header */
> | // At this point, 'size_t' is NOT in the 'std' namespace
> |
> |
> | The bottom line here is this: the unqualified name 'size_t' should be
> | visible within the global namespace of a C++ program if either the C++
> | header <cstddef> or the C header <stddef.h> is specified.
>
> I believe you have it backwards according to the standard. However
> your interpretation is fairly close to common practice.
>
> From the C++ standard 17.4.1.2:
>
> | -4- Except as noted in clauses lib.language.support through
> | lib.input.output , the contents of each header cname shall be the same
> | as that of the corresponding header name .h , as specified in ISO/IEC
> | 9899:1990 Programming Languages C (Clause 7), or ISO/IEC:1990
> | Programming Languages --- C AMENDMENT 1: C Integrity, (Clause 7), as
> | appropriate, as if by inclusion. In the C++ Standard Library, however,
> | the declarations and definitions (except for names which are defined as
> | macros in C) are within namespace scope ( basic.scope.namespace ) of
> | the namespace std.
>
> | -7- depr.c.headers , Standard C library headers, describes the effects
> | of using the name .h (C header) form in a C++ program.*
> | [Footnote: The ".h" headers dump all their names into the global
> | namespace, whereas the newer forms keep their names in namespace std .
> | Therefore, the newer forms are the preferred forms for all uses except
> | for C++ programs which are intended to be strictly compatible with C.
> | --- end foonote]


The C++ standard also says:

<quote>
D.5 Standard C Library headers

2. 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 within the namespace scope of the namespace std
and is followed by an explicit using-declaration (7.3.3)
</quote>

See also item 143 in Rev. 25 of the "C++ Standard Library Closed Issues
List" re. clarification of the meaning of paragraph D.5/2:

http://anubis.dkuug.dk/jtc1/sc22/wg2...wg-closed.html


So as stated in 17.4.1.2/4 and D.5/2, if a C++ translation unit
#include's the header <cstdio>, this places the name 'printf' (for
example) in the 'std' namespace. The <cstdio> header *also* provides a
using-declaration for 'printf' in the global namespace IAW D.5/2:

namespace std { int printf(const char*, ...); }
using std:rintf;

This is why my "Hello, World" code sample (see my previous reply) is not
rejected by the C++ compiler.

FWIW, my "Hello, World" code compiles fine with Comeau C/C++ 4.3.1 and
GNU G++ 3.3. Neither compiler complains that the unqualified name
'printf' is undefined in function main(). So <cstdio> has apparently
introduced the name 'printf' into the global namespace via a
using-declaration IAW paragraph D.5/2.

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com


 
Reply With Quote
 
Howard Hinnant
Guest
Posts: n/a
 
      06-30-2003
In article <v9QLa.4012$(E-Mail Removed)2.webusenet.com>, Jim Fischer
<(E-Mail Removed)> wrote:

| > | For example,
| > |
| > | #include <cstddef> // ISO C++ header
| > |
| > | The C++ header 'cstddef' introduces the name 'size_t' (for example) into
| > | C++'s 'std' namespace, and follows it with an explicit using-declaration
| > | (D5/3), i.e.,
|
| [Correction: I quoted D.5/3 here, but I probably should have quoted
| D.5/2 instead. Paragraph D.5/3 is an "Example" paragraph for D.5/2.]

D.5/2-3 are discussing the name.h (deprecated) headers, not the cname
headers.

| > | namespace std { typedef {whatever} size_t; }
| > | using std::size_t;

And I'm not sure exactly where you're getting your quotes from. They
don't reflect what I'm reading in ISO/IEC 14882:1998(E)

| <quote>
| D.5 Standard C Library headers
|
| 2. 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 within the namespace scope of the namespace std
| and is followed by an explicit using-declaration (7.3.3)
| </quote>

Now this text I recognize. But how does this support your position?
It is discussing what name.h does, not what cname does.

| See also item 143 in Rev. 25 of the "C++ Standard Library Closed Issues
| List" re. clarification of the meaning of paragraph D.5/2:
|
| http://anubis.dkuug.dk/jtc1/sc22/wg2...wg-closed.html

Yes, I participated in this discussion in Kona (Oct 20-26, 1999), but
missed the following meeting in Tokyo. Note that the issue has been
marked NAD (Not A Defect), and thus no change to the standard has been
made.

| So as stated in 17.4.1.2/4 and D.5/2, if a C++ translation unit
| #include's the header <cstdio>, this places the name 'printf' (for
| example) in the 'std' namespace.

Right so far.

| The <cstdio> header *also* provides a
| using-declaration for 'printf' in the global namespace IAW D.5/2:
|
| namespace std { int printf(const char*, ...); }
| using std:rintf;

D.5/2 says that <stdio.h> does this, not <cstdio>. Please reread all
three paragraphs of D.5.

| This is why my "Hello, World" code sample (see my previous reply) is not
| rejected by the C++ compiler.
|
| FWIW, my "Hello, World" code compiles fine with Comeau C/C++ 4.3.1 and
| GNU G++ 3.3. Neither compiler complains that the unqualified name
| 'printf' is undefined in function main(). So <cstdio> has apparently
| introduced the name 'printf' into the global namespace via a
| using-declaration IAW paragraph D.5/2.

Yes, these platforms do as you say. But it is not what D.5/2 says.
The paragraphs you quote are not supporting the conclusions you are
drawing.

I was going to suggest that if you're still unclear on this that you
post it to comp.std.c++. But it has already been discussed many times
there. A newsgroup search on name.h and cname will yield plenty to
read.

--
Howard Hinnant
Metrowerks
 
Reply With Quote
 
Jim Fischer
Guest
Posts: n/a
 
      06-30-2003
So after reading Howard's and Dietmar's comments (thanks for those,
BTW), and after some digging around in the comp.std.c++ archives, here
is the latest iteration in my understanding of the C library headers in
a C++ program:


1) Section D.5 of the C++ standard deals with the depricated <name.h> C
headers and not the <cname> C++ headers. I don't know how/why I got it
into my head that D.5 dealt with the <cname> headers. <?> I obviously
wan't paying attention. Musta played too much golf this weekend...


2) In a nutshell, the <cname> headers introduce names from the C library
into the 'std::' namespace but not into the global namespace.
Consequently, the following program is nonconforming because the
unqualified name 'printf' should not be declared in the global namespace:

#include <cstdio>
int main() { printf("Hello"); }


3) The meaning of paragraph D.5/2 in the C++ standard,

<quote>
D.5/2. 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 within the namespace scope of the namespace
std and is followed by an explicit using-declaration (7.3.3)
</quote>

is this: If a C++ translation unit uses,

#include <stdio.h>

a conforming implementation defines the name 'printf' (for example) in
the 'std::' namespace, and also provides a using-declaration that
aliases the name 'printf' in the global namespace IAW D.5/2, e.g.,

namespace std { int printf(const char *, ...); }
using std:rintf;

[n.b. I'm ignoring for the moment all of the side issues that crop up
when incorporating the C library into the C++ language -- e.g.,
converting macros into actual functions for type checking purposes,
const-correctnes, etc.]


4) In some actual standard C++ libraries, the <cname> headers #include
the corresponding C library <name.h> headers into the global namespace,
and then employ using-declarations (for example) to introduce the C
library names into the std:: namespace, e.g.,

// <cstdio>
...
#include <stdio.h>
namespace std {
using :rintf;
}
...

While technically nonconforming, use of this technique in actual
standard C++ libraries is not uncommon.

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reinterpret_cast<std::size_t>(p) and reinterpret_cast<std::size_t&>() Alex Vinokur C++ 1 02-06-2011 07:48 AM
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
auto_ptr to char[ size_t]? Marc Schellens C++ 8 07-17-2003 09:11 AM
Re: for(size_t a=begin();a!=end();++a){} Chris \( Val \) C++ 2 07-14-2003 06:31 AM
Re: size_t ... standards Howard Hinnant C++ 0 06-29-2003 05:45 PM



Advertisments