473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

why ::size_t and ::ptrdiff_t aren't undefined in <cstddef>

std::size_t and std::ptrdiff_t are defined in <cstddef>.

I've noticed that a bunch of old math function in the global namespace
are undefined in <cmath> and they are redefined in std namespace.

I'm wondering why ::size_t and ::ptrdiff_t aren't undefined.

Thanks,
Peng

Oct 19 '05 #1
9 3080
Pe*******@gmail.com wrote:
std::size_t and std::ptrdiff_t are defined in <cstddef>.

I've noticed that a bunch of old math function in the global namespace
are undefined in <cmath> and they are redefined in std namespace.

I'm wondering why ::size_t and ::ptrdiff_t aren't undefined.


I can't find any specific mention of std::ptrdiff_t. Are you sure that
that type is in the 'std' namespace?

IIRC, it was discussed several times, and the conclusion is that it is
generally quite a task to keep (a) <cHHHH> and <HHHH.h> headers in sync
and (b) ready for inclusion by both C and C++ translation units. That
is why often while the requirement is in <cHHHH> to only declare those
functions/types in 'std' namespace, the global definitions do slip in.

V
Oct 19 '05 #2
On 18 Oct 2005 19:27:15 -0700, "Pe*******@gmail.com"
<Pe*******@gmail.com> wrote in comp.lang.c++:
std::size_t and std::ptrdiff_t are defined in <cstddef>.

I've noticed that a bunch of old math function in the global namespace
are undefined in <cmath> and they are redefined in std namespace.

I'm wondering why ::size_t and ::ptrdiff_t aren't undefined.

Thanks,
Peng


When you include any of the 18 C standard headers that are included in
the C++ standard, you can do so in two ways:

#include <stddef.h>

....or:

#include <cstddef>

This applies to every one of the C headers.

When you include a C header the first way, the same way as you would
in C, the C++ standard requires that each identifier that can be
scoped be defined in both the global and std namespace.

When you include a C header the second way, that is 'c' in front and
without the ".h" at the end, the C++ standard requires that each such
identifier be defined in the std namespace only, and not in the global
namespace.

The issue here is that this particular requirement of the C standard
is not highly regarded by many. Quite a few compiler vendors put some
of all of the C identifiers in the global namespace even when the
headers are included with the preferred C++ syntax. They think they
will get too many complaints from user about breaking existing code.

There is another possibility. The C++ standard allows any standard
header to include any other standard headers. There are, or at least
have been, some C++ implementations where some C++ standard headers
include C headers, often stddef.h, with the C syntax, thus putting
those names into the global namespace.

Technically, this is a violation of the standard, but it is unlikely
to get much attention from the implementers.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Oct 19 '05 #3

Victor Bazarov wrote:
Pe*******@gmail.com wrote:
std::size_t and std::ptrdiff_t are defined in <cstddef>.

I've noticed that a bunch of old math function in the global namespace
are undefined in <cmath> and they are redefined in std namespace.

I'm wondering why ::size_t and ::ptrdiff_t aren't undefined.
I can't find any specific mention of std::ptrdiff_t. Are you sure that
that type is in the 'std' namespace?

Yes. You can read the file cstddef by yourself.
IIRC, it was discussed several times, and the conclusion is that it is
generally quite a task to keep (a) <cHHHH> and <HHHH.h> headers in sync
and (b) ready for inclusion by both C and C++ translation units. That
is why often while the requirement is in <cHHHH> to only declare those
functions/types in 'std' namespace, the global definitions do slip in.


If cmath chooses to undef the old definitions, for consistency, should
cstddef also undefine old definitions as well?

Oct 19 '05 #4
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:4p********************************@4ax.com...
On 18 Oct 2005 19:27:15 -0700, "Pe*******@gmail.com"
<Pe*******@gmail.com> wrote in comp.lang.c++:
std::size_t and std::ptrdiff_t are defined in <cstddef>.

I've noticed that a bunch of old math function in the global namespace
are undefined in <cmath> and they are redefined in std namespace.

I'm wondering why ::size_t and ::ptrdiff_t aren't undefined.

Thanks,
Peng
When you include any of the 18 C standard headers that are included in
the C++ standard, you can do so in two ways:

#include <stddef.h>

...or:

#include <cstddef>

This applies to every one of the C headers.

When you include a C header the first way, the same way as you would
in C, the C++ standard requires that each identifier that can be
scoped be defined in both the global and std namespace.

When you include a C header the second way, that is 'c' in front and
without the ".h" at the end, the C++ standard requires that each such
identifier be defined in the std namespace only, and not in the global
namespace.

The issue here is that this particular requirement of the C standard
is not highly regarded by many. Quite a few compiler vendors put some
of all of the C identifiers in the global namespace even when the
headers are included with the preferred C++ syntax. They think they
will get too many complaints from user about breaking existing code.


No, the reasons go much deeper than that.
There is another possibility. The C++ standard allows any standard
header to include any other standard headers. There are, or at least
have been, some C++ implementations where some C++ standard headers
include C headers, often stddef.h, with the C syntax, thus putting
those names into the global namespace.

Technically, this is a violation of the standard, but it is unlikely
to get much attention from the implementers.


No, it's not.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Oct 19 '05 #5
In article <Zc********************@comcast.com>,
Victor Bazarov <v.********@comAcast.net> wrote:
Pe*******@gmail.com wrote:
std::size_t and std::ptrdiff_t are defined in <cstddef>.

I've noticed that a bunch of old math function in the global namespace
are undefined in <cmath> and they are redefined in std namespace.

I'm wondering why ::size_t and ::ptrdiff_t aren't undefined.
I can't find any specific mention of std::ptrdiff_t. Are you sure that
that type is in the 'std' namespace?


See 17.4.3.1.4
IIRC, it was discussed several times, and the conclusion is that it is
generally quite a task to keep (a) <cHHHH> and <HHHH.h> headers in sync
and (b) ready for inclusion by both C and C++ translation units. That
is why often while the requirement is in <cHHHH> to only declare those
functions/types in 'std' namespace, the global definitions do slip in.


This probably hits home why although the underlying reason
for blah.h vs cblah was noble and "the right thing to do"
that in practice it was a mistake. So while there is lots
of noise over say export, IMO, these are some of the real
issues which need to be addressed (and to be fair, they are
being addressed).
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 19 '05 #6
Pe*******@gmail.com wrote:
Victor Bazarov wrote:
Pe*******@gmail.com wrote:
std::size_t and std::ptrdiff_t are defined in <cstddef>.

I've noticed that a bunch of old math function in the global namespace
are undefined in <cmath> and they are redefined in std namespace.

I'm wondering why ::size_t and ::ptrdiff_t aren't undefined.


I can't find any specific mention of std::ptrdiff_t. Are you sure that
that type is in the 'std' namespace?


Yes. You can read the file cstddef by yourself.


Well, it can be educational to read "the file cstddef", but it is more
educational to read the Stadnard. The contents of _your_ cstddef can very
well be different from the contents of _my_ cstddef. [Not to mention that
standard headers don't have to be implemented as files]
IIRC, it was discussed several times, and the conclusion is that it is
generally quite a task to keep (a) <cHHHH> and <HHHH.h> headers in sync
and (b) ready for inclusion by both C and C++ translation units. That
is why often while the requirement is in <cHHHH> to only declare those
functions/types in 'std' namespace, the global definitions do slip in.

If cmath chooses to undef the old definitions, for consistency, should
cstddef also undefine old definitions as well?


I don't know. The Standard does say that <cHHHH> should declare
everything in 'std' namespace, while <HHHH.h> should have both 'std' and
globally declared variations for each standard symbol. There is nothing
in the Standard about _un_declaring anything.

Again, it *has* been discussed before, look in the archives. The result
as I see it, of those discussions, is that it's fairly difficult to do
what the Standard asks, so some symbols (like 'size_t', for example) can
sometimes fall through. It could be seen as a bug or it could be seen as
a sign of things to come. Ask in comp.std.c++ about that.

V
Oct 19 '05 #7
In article <4p********************************@4ax.com>,
Jack Klein <ja*******@spamcop.net> wrote:
When you include any of the 18 C standard headers that are included in
the C++ standard, you can do so in two ways:

#include <stddef.h>

...or:

#include <cstddef>

This applies to every one of the C headers.

When you include a C header the first way, the same way as you would
in C, the C++ standard requires that each identifier that can be
scoped be defined in both the global and std namespace.

When you include a C header the second way, that is 'c' in front and
without the ".h" at the end, the C++ standard requires that each such
identifier be defined in the std namespace only, and not in the global
namespace.

The issue here is that this particular requirement of the C standard
is not highly regarded by many.
Not sure what this mean, as I have never heard this to be the case.
Quite a few compiler vendors put some
of all of the C identifiers in the global namespace even when the
headers are included with the preferred C++ syntax. They think they
will get too many complaints from user about breaking existing code.
This is an issue, but probably not the top most one, that probably
being the consideration of the various forms of implementations out
there.
There is another possibility. The C++ standard allows any standard
header to include any other standard headers. There are, or at least
have been, some C++ implementations where some C++ standard headers
include C headers, often stddef.h, with the C syntax, thus putting
those names into the global namespace.

Technically, this is a violation of the standard, but it is unlikely
to get much attention from the implementers.


Neither of these points seem to be true. I suspect we may be
cross talking past the same issue somehow...
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 19 '05 #8
On Tue, 18 Oct 2005 22:10:44 -0500, Jack Klein <ja*******@spamcop.net>
wrote in comp.lang.c++:

[snip something obviously complete wrong...]

Since both P.J. Plauger and Greg Comeau tell me I'm wrong in
comp.lang.c++, I'm willing to throw in the towel. Ignore my previous
post.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Oct 20 '05 #9
In article <ub********************************@4ax.com>,
Jack Klein <ja*******@spamcop.net> wrote:
On Tue, 18 Oct 2005 22:10:44 -0500, Jack Klein <ja*******@spamcop.net>
wrote in comp.lang.c++:

[snip something obviously complete wrong...]

Since both P.J. Plauger and Greg Comeau tell me I'm wrong in
comp.lang.c++, I'm willing to throw in the towel. Ignore my previous
post.
Well, if you are wrong, it looks like a defect in the standard to me.
17.4.1.1p2 says:
-2- All library entities except macros, operator
new and operator delete are defined within the
namespace std or namespaces nested within namespace std.


Admittedly it does not say "and nowhere else". But reading that
paragraph with the lack of "and nowhere else" and interpreting that to
mean "and anywhere else you please" is stretching things pretty far imho.

That being said, there is also a proposal before the C++ committee to
standardize the widespread practice of "leaking" the C names to global:

http://www.open-std.org/jtc1/sc22/wg...ctive.html#456

The status of this proposal is "Open" meaning that it has not yet been
decided upon one way or the other.

-Howard
Oct 25 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: matthurne | last post by:
I'm doing a chapter 12 exercise from Accelerated C++ ... writing a string-like class which stores its data in a low-level way. My class, called Str, uses a char array and length variable. I've...
37
by: Zombie | last post by:
Hi, what is the correct way of converting contents of a <string> to lowercase? There are no methods of <string> class to do this so I fallback on strlwr(). But the c_str() method returns a const...
822
by: Turamnvia Suouriviaskimatta | last post by:
I 'm following various posting in "comp.lang.ada, comp.lang.c++ , comp.realtime, comp.software-eng" groups regarding selection of a programming language of C, C++ or Ada for safety critical...
3
by: H. S. | last post by:
Hi, I am trying to compile these set of C++ files and trying out class inheritence and function pointers. Can anybody shed some light why my compiler is not compiling them and where I am going...
11
by: Martin Jørgensen | last post by:
Hi, - - - - - - - - - - - - - - - #include <iostream> #include <string> #include <map> using namespace std; int main() {
6
by: davidb | last post by:
Hi, does someone know how to get the length of a 2 dimensional string array: here what i need: ---------------------------------------------------------------- char **getList(void){ char...
8
by: barcaroller | last post by:
I have a pointer to a memory block. Is there a way I can map a vector<Tto this memory block? I would like to take advantage of the powerful vector<T> member functions. Please note that I cannot...
1
by: perroe | last post by:
Hi I have a array of complex numbers that are stored in a simple double array. This is done since the array is part of an wrapper for an external C library, and the imaginary part of the first...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.