Topic: some questions about typedef
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/01/20 Raw View
marty Holcomb wrote:
>
> Mark Rodgers wrote:
> >> typedef char* T;
> >>
> >>Is the declaration of g interpreted as
> >>
> >> void g(const char* y);
> >
> >No
> >
> >>or as
> >>
> >> void g(char* const y);
> >
> >Yes
>
> What is the difference in these two examples? Further down in your post
> you say:
>
> >Alternatively, imagine typedefs introduce parentheses into our declaration
> >algebra:
> >
> > const T
> >= const (char *)
> >= (char *) const
> >= char * const
> >
>
> So...const char * y is the same as char * const y ....right?
No, const char * y is (const char) * y, that is, a pointer to a constant
character, while char * const y is (char *) const y, that is, a constant
pointer to a character.
> >Life would be a lot simpler if people always wrote the const on the right.
> >"char const *" is much easier to read (right to left as "pointer to
> >constant char") and would save confusion with "char * const" ("constant
> pointer to
> >char").
> >
>
> I don't see where you write a "pointer to constant char" in your post.
> Please explain if I am missing something...
>
char const * y is a pointer to constant char. Try to read it from right
to left: y (is a) pointer-to const char.
char * const y then gets: y (is a) const pointer-to char.
The "most constant" possibility is then:
char const * const y: y is a constant pointer to constant char
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: marty Holcomb <mholcomb@BIX.com>
Date: 1997/12/31 Raw View
Max Polk wrote:
>> Why #include <fstream.h> when you're not using it?.....you
>> are using iostream.h without #include(ing) it.
>
>Isn't it true that when you include fstream.h, you AUTOMATICALLY get
>iostream.h?
>
A common practice I have developed over the years is to include only the
header files that I use in a module. But, include _all_ the header files
you need, even if another header file includes it as well. This seems to
help ease maintenance issues should you redesign the header file that
includes something now, but later removes it.
YMMV.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: ziporama@aol.com (Ziporama)
Date: 1997/12/29 Raw View
Paul D. DeRocco writes:
>>
>> I hope my understanding of English was not deficient: (from p.806 of
>> Stroustrup 3rd)
>>
>> The list of specifiers that starts a declaration cannot be empty (there
>> is no "implicit int;" B.2) and consists of the longest possible sequence of
>> specifiers. For example:
>>
>> typedef int I;
>> void f(unsigned I) { /* ... */ }
>>
>> Here, f() takes an unnamed unsigned int.
>
>Wow. That looks wrong to me, too. I gave the following to Borland 5.01:
>
> #include <fstream.h>
> typedef char C;
> void f(unsigned C) { cout << sizeof(C); }
> int main() { f(0); }
>
This is funny.....i don't know anything, but unsigned is an int, unless
explicitly declared unsigned char.....on my win95, if you hand sizeof() an int,
you get 4(bytes)......so when you hand f() 0, cout gives you 4.
If instead of f(unsigned C), you give f(C), you get the 1 you
expected.......and since int main() returns int, you might want to end main:
int main()
{
// stuff
return 0;
}
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: ziporama@aol.com (Ziporama)
Date: 1997/12/29 Raw View
Paul D. DeRocco writes:
>I gave the following to Borland 5.01:
>
> #include <fstream.h>
> typedef char C;
> void f(unsigned C) { cout << sizeof(C); }
> int main() { f(0); }
>
>It wrote "4", meaning that it thought C was an unsigned parameter, not part
>of the type of an unamed parameter. Had it worked the way BS says it's
>supposed to, it would have written "1".
i forgot to add: Why #include <fstream.h> when you're not using it?.....you
are using iostream.h without #include(ing) it.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: polk@sprintmail.com (Max Polk)
Date: 1997/12/31 Raw View
> Why #include <fstream.h> when you're not using it?.....you
> are using iostream.h without #include(ing) it.
Isn't it true that when you include fstream.h, you AUTOMATICALLY get
iostream.h?
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Hyman Rosen <cardboard.mti.sgi.com!sgi.com!ncar.UCAR.EDU!uunet!jyacc!hymie>
Date: 1997/12/15 Raw View
marty Holcomb <mholcomb@BIX.com> writes:
> Valentin Bonnard wrote:
> >typedef int INT;
> >signed INT i1;
>
> is this invalid because you really have signed (signed int) i1;
> because the int is really a signed int?
It is invalid because you may not apply long/short/signed/unsigned
to typedef'ed names.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1997/12/17 Raw View
Hyman Rosen wrote:
>
> > >typedef int INT;
> > >signed INT i1;
>
> It is invalid because you may not apply long/short/signed/unsigned
> to typedef'ed names.
Can you quote chapter and verse from CD2? I couldn't find it. The grammar seems
to suggest that even ridiculous things like:
int float x;
are syntactically legal, leaving it up to the semantic interpretation to reject
them, but I couldn't find out where in CD2 it nailed down whether modifiers
like signed and unsigned could or couldn't be combined with typedefs for
built-in types like int and char.
--
Ciao,
Paul
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Hyman Rosen <uunet!jyacc!hymie@ncar.UCAR.EDU>
Date: 1997/12/18 Raw View
"Paul D. DeRocco" <pderocco@ix.netcom.com> writes:
> Hyman Rosen wrote:
> > It is invalid because you may not apply long/short/signed/unsigned
> > to typedef'ed names.
>
> Can you quote chapter and verse from CD2?
Sure. Look at 7.1.5.2, especially at Table 1.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: stanliao@synopsys.com (Stan Y. Liao)
Date: 1997/12/11 Raw View
When typedef is used with const/volatile qualifiers and pointers, how
should it be interpreted? For example,
typedef char* T;
void g(const T y);
Is the declaration of g interpreted as
void g(const char* y);
or as
void g(char* const y);
If we look at the on p.806 of Stroustrup 3rd Ed., we see that:
typdef int I;
void f(unsigned I);
is interpreted as:
void f(unsigned int);
This is a little bit counterintuitive (to me at least).
Any thoughts?
Regards,
Stan
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Mark Rodgers" <mark.rodgers@xtra.co.nz>
Date: 1997/12/12 Raw View
Stan Y. Liao wrote in message ...
>When typedef is used with const/volatile qualifiers and pointers, how
>should it be interpreted? For example,
>
> typedef char* T;
>
>Is the declaration of g interpreted as
>
> void g(const char* y);
No
>or as
>
> void g(char* const y);
Yes
Read C++ declarations right to left and its better to swap const around so
it is on the right. (const is commutative).
Therefore,
const T
= T const
= char * const
Alternatively, imagine typedefs introduce parentheses into our declaration
algebra:
const T
= const (char *)
= (char *) const
= char * const
Life would be a lot simpler if people always wrote the const on the right.
"char const *" is much easier to read (right to left as "pointer to constant
char") and would save confusion with "char * const" ("constant pointer to
char").
Unfortunately the convention of const on the left, except after a * is too
ingrained.
Mark
mark.rodgers@xtra.co.nz
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/12/12 Raw View
Stan Y. Liao wrote:
>
> When typedef is used with const/volatile qualifiers and pointers, how
> should it be interpreted? For example,
>
> typedef char* T;
> void g(const T y);
>
typedef introduce a type, not a macro (typedef is better than
#define).
cv-qualification apply to the type named T, otherwise it would
be a terrible mess (you would have to know the type between every
typedef).
> Is the declaration of g interpreted as
> void g(char* const y);
yes
> If we look at the on p.806 of Stroustrup 3rd Ed., we see that:
I think you misread C++PL, but perhaps there is a bug.
> typdef int I;
> void f(unsigned I);
>
> is interpreted as:
>
> void f(unsigned int);
No it is _not_:
typedef int INT;
typdef long LONG;
LONG LONG l1;
long LONG l2;
signed INT i1;
unsigned LONG l3;
all these declaration are invalid (resp C/C++/C9X).
> This is a little bit counterintuitive (to me at least).
It would be strange.
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Stan Y. Liao" <stanliao@synopsys.com>
Date: 1997/12/12 Raw View
>Stan Y. Liao wrote:
>>
>> When typedef is used with const/volatile qualifiers and pointers, how
>> should it be interpreted? For example,
>>
>> typedef char* T;
>> void g(const T y);
>>
>
>typedef introduce a type, not a macro (typedef is better than
>#define).
>
>cv-qualification apply to the type named T, otherwise it would
>be a terrible mess (you would have to know the type between every
>typedef).
>
>> Is the declaration of g interpreted as
>
>> void g(char* const y);
>
>yes
This is what I thought. But I was confused by what follows (below).
>
>> If we look at the on p.806 of Stroustrup 3rd Ed., we see that:
>
>I think you misread C++PL, but perhaps there is a bug.
>
>> typdef int I;
>> void f(unsigned I);
>>
>> is interpreted as:
>>
>> void f(unsigned int);
>
>No it is _not_:
>
>typedef int INT;
>typdef long LONG;
>LONG LONG l1;
>long LONG l2;
>signed INT i1;
>unsigned LONG l3;
>
>all these declaration are invalid (resp C/C++/C9X).
>
I hope my understanding of English was not deficient: (from p.806 of
Stroustrup 3rd)
The list of specifiers that starts a declaration cannot be empty (there
is no "implicit int;" B.2) and consists of the longest possible sequence of
specifiers. For example:
typedef int I;
void f(unsigned I) { /* ... */ }
Here, f() takes an unnamed unsigned int.
-- Stan
Stan Y. Liao Phone: +1 650.694.1912
Synopsys, Inc. Fax: +1 650.694.1626
700 E. Middlefield Rd. MS 14.2 Email: stanliao@synopsys.com
Mountain View, CA 94043-4033 StanLiao.91@alum.mit.edu
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1997/12/13 Raw View
Stan Y. Liao wrote:
>
> I hope my understanding of English was not deficient: (from p.806 of
> Stroustrup 3rd)
>
> The list of specifiers that starts a declaration cannot be empty (there
> is no "implicit int;" B.2) and consists of the longest possible sequence of
> specifiers. For example:
>
> typedef int I;
> void f(unsigned I) { /* ... */ }
>
> Here, f() takes an unnamed unsigned int.
Wow. That looks wrong to me, too. I gave the following to Borland 5.01:
#include <fstream.h>
typedef char C;
void f(unsigned C) { cout << sizeof(C); }
int main() { f(0); }
It wrote "4", meaning that it thought C was an unsigned parameter, not part of
the type of an unamed parameter. Had it worked the way BS says it's supposed
to, it would have written "1".
I looked all through CD2 7.1.5, and I couldn't find anything that nailed down
whether "unsigned" could be combined only with "int" or with any typedef for
"int".
Bjarne, are you reading this? Was that a bug on p.806?
--
Ciao,
Paul
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1997/12/13 Raw View
Stan Y. Liao <stanliao@synopsys.com> wrote:
: When typedef is used with const/volatile qualifiers and pointers, how
: should it be interpreted? For example,
: typedef char* T;
: void g(const T y);
: Is the declaration of g interpreted as
: void g(const char* y);
: or as
: void g(char* const y);
The later.
Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Pete Becker <petebecker@acm.org>
Date: 1997/12/13 Raw View
Mark Rodgers wrote:
>
>
> Read C++ declarations right to left and its better to swap const around so
> it is on the right. (const is commutative).
>
> Therefore,
> const T
> = T const
> = char * const
Const is not "commutative", even with a rather loose interpretation of
that word. 'char *const' and 'char const *' mean to very different
things. It is correct that 'const char' and 'char const' mean the same
thing, but that is the only situation in which you can move const
around: when you're applying it to the underlying type.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: marty Holcomb <mholcomb@BIX.com>
Date: 1997/12/13 Raw View
Mark Rodgers wrote:
>> typedef char* T;
>>
>>Is the declaration of g interpreted as
>>
>> void g(const char* y);
>
>No
>
>>or as
>>
>> void g(char* const y);
>
>Yes
What is the difference in these two examples? Further down in your post
you say:
>Alternatively, imagine typedefs introduce parentheses into our declaration
>algebra:
>
> const T
>= const (char *)
>= (char *) const
>= char * const
>
So...const char * y is the same as char * const y ....right?
>Life would be a lot simpler if people always wrote the const on the right.
>"char const *" is much easier to read (right to left as "pointer to
>constant char") and would save confusion with "char * const" ("constant
pointer to
>char").
>
I don't see where you write a "pointer to constant char" in your post.
Please explain if I am missing something...
Marty
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: marty Holcomb <mholcomb@BIX.com>
Date: 1997/12/13 Raw View
Valentin Bonnard wrote:
>typedef int INT;
>signed INT i1;
is this invalid because you really have signed (signed int) i1;
because the int is really a signed int?
Marty
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]