C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: size_t ... standards

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Howard Hinnant
Guest





PostPosted: Sun Jun 29, 2003 5:34 pm    Post subject: Re: size_t ... standards Reply with quote



In article <bdn2gu$987$1$830fa78d (AT) news (DOT) demon.co.uk>, <blue (AT) id-base (DOT) com>
wrote:

Quote:
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

Back to top
Howard Hinnant
Guest





PostPosted: Sun Jun 29, 2003 5:45 pm    Post subject: Re: size_t ... standards Reply with quote



In article <hfELa.34882$pH3.34341 (AT) news2 (DOT) east.cox.net>, MiniDisc_2k2
<MattDelB (AT) nospam (DOT) com> wrote:

Quote:
Of course,
I wouldn't worry about including the right header because they all define
size_t as a typedef to unsigned int.

That's not a very good assumption for portability. size_t must be a
typedef to the same type that a sizeof() expression results in. That
type must be able to represent the largest array types the platform is
capable of supporting, and must also be unsigned.

Here is a way to investigate the matter on your platform:

#include <iostream>
#include <typeinfo>
#include <stddef.h>

int main()
{
std::cout << "typeof(sizeof) = " << typeid(sizeof(0)).name() << 'n';
std::cout << "typeof(size_t) = " << typeid(size_t).name() << 'n';
}

On the platform on which I tested this code it resulted in:

typeof(sizeof) = unsigned long
typeof(size_t) = unsigned long

If it doesn't print out the same type in both statements, then your C++
compiler (or library) has a bug.

--
Howard Hinnant
Metrowerks

Back to top
Jim Fischer
Guest





PostPosted: Mon Jun 30, 2003 1:38 am    Post subject: Re: size_t ... standards Reply with quote



Howard Hinnant wrote:
Quote:
In article <bdn2gu$987$1$830fa78d (AT) news (DOT) demon.co.uk>, <blue (AT) id-base (DOT) com
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 | 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::printf':

#include <cstdio>
int main() {
printf("Hello,Worldn"); // 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



Back to top
Howard Hinnant
Guest





PostPosted: Mon Jun 30, 2003 1:54 am    Post subject: Re: size_t ... standards Reply with quote

In article <pnMLa.3624$E5.1005 (AT) fe01 (DOT) atl2.webusenet.com>, Jim Fischer
<jfischer_link5809 (AT) now (DOT) here.com> wrote:

Quote:
Howard Hinnant wrote:
In article <bdn2gu$987$1$830fa78d (AT) news (DOT) demon.co.uk>, <blue (AT) id-base (DOT) com
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 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::printf':

#include <cstdio
int main() {
printf("Hello,Worldn"); // 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 // 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:

Quote:
-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

Back to top
Jim Fischer
Guest





PostPosted: Mon Jun 30, 2003 5:56 am    Post subject: Re: size_t ... standards Reply with quote

Howard Hinnant wrote:
Quote:
In article <pnMLa.3624$E5.1005 (AT) fe01 (DOT) atl2.webusenet.com>, Jim Fischer
[email]jfischer_link5809 (AT) now (DOT) here.com[/email]> wrote:

| Howard Hinnant wrote:
| > In article <bdn2gu$987$1$830fa78d (AT) news (DOT) demon.co.uk>, <blue (AT) id-base (DOT) com
| > 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.]


Quote:
|
| 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::printf':
|
| #include <cstdio
| int main() {
| printf("Hello,Worldn"); // 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 | // 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/wg21/docs/lwg-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::printf;

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



Back to top
Howard Hinnant
Guest





PostPosted: Mon Jun 30, 2003 1:10 pm    Post subject: Re: size_t ... standards Reply with quote

In article <v9QLa.4012$E5.2878 (AT) fe01 (DOT) atl2.webusenet.com>, Jim Fischer
<jfischer_link5809 (AT) now (DOT) here.com> wrote:

Quote:
| 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.

Quote:
| 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:
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.

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/wg21/docs/lwg-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.

Quote:
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.

Quote:
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::printf;

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

Quote:
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

Back to top
Jim Fischer
Guest





PostPosted: Mon Jun 30, 2003 7:22 pm    Post subject: Re: size_t ... standards Reply with quote

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::printf;

[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 ::printf;
}
...

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


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.