Topic: const in arguments
Author: Michel Michaud <micm19@gest-netware.cstjean.qc.ca>
Date: 2000/03/09 Raw View
Ron Natalie wrote :
>
> Michel Michaud wrote:
> >
> >
> > But I would immediatly reconsider if there was one
> > "authorative" book that would start using the
> > left
||||
OUPS !
> > style. Who of Stroustrup, Meyers, Koenig, Lippman,
> > Sutter, Coplien, etc. will take up the challenge?
> >
>
> Stroustrup puts it on the left. Frankly, as I've said before, I can't
> get worked up over it. You need to know that they can go in either
> order. Frankly, even though it's not my pick of styles, I recommend
> that if nothing else people use Stroustrup's style. At least most
> of us C++ gurus are comfortable reading it.
I meant "if there was one authorative book that would STOP using the
left style" or "if there was one authorative book that would start
using the RIGHT style".
You're right we should know it can be put on both sides because the
language allows it. But it would be "better" if only the right side
was allowed, because it would be consistent (it is a single exception
to the rule). It is much too late for that now, of course.
Ans yes, it cannot be that bad to go "guru" style in whatever
situation, except when the gurus are plain wrong... Unfortunatly
I know a number of places where the best gurus have been wrong, and
I know a few self-proclaimed gurus that are systematically wrong!
(I took an online test on C++ and was classified as "almost guru",
but, boy, am I wrong at times... I would certainly never say I am
one! In fact, I would be suspicious at anyone proclaiming he/she
is. But we do have to follow some authors' ideas at time...)
--
Michel Michaud (micm19@removethis.mail2.cstjean.qc.ca)
http://www3.sympatico.ca/michel.michaud
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: jpotter@falcon.lhup.edu (John Potter)
Date: 2000/03/09 Raw View
On Wed, 8 Mar 2000 06:27:36 CST, David R Tribble <david@tribble.com>
wrote:
: So how do you explain how to place 'mutable'?
It's easy. The position of mutable is static. The position of
const/volatile is dynamic. One sits still and the other moves.
Pun on static intended.
John
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/03/09 Raw View
In article <38C536B1.595DBE51@tribble.com>, David R Tribble
<david@tribble.com> writes
>So how do you explain how to place 'mutable'?
But I thought that mutable was a top level qualifier only and so it
makes sense to stick it out to the left (or else you would have to hide
it away at the extreme right) mutable is not the opposite of const.
Francis Glassborow Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: David R Tribble <david@tribble.com>
Date: 2000/03/15 Raw View
Francis Glassborow wrote:
>
> David R Tribble <david@tribble.com> writes:
>> So how do you explain how to place 'mutable'?
>
> But I thought that mutable was a top level qualifier only and so it
> makes sense to stick it out to the left (or else you would have to
> hide it away at the extreme right) mutable is not the opposite of
> const.
'const' and 'mutable' certainly are opposites when applied to
class members.
class Foo
{
int f() const; // const func
mutable int m; // mutable var
};
In this case, the placement of 'const' is just as rigid as the
placement of 'mutable'. What's frustrating is that the placements
are also total opposites - one is at the far left of the declaration
and the other is at the far right.
-- David R. Tribble, david@tribble.com, http://david.tribble.com --
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: 2000/02/25 Raw View
Paulo Pinto wrote:
>
> Is this second form, now the official way to declare arguments that
> are const ?
>
There is NO official way of doing it. C++ has always supported the const
declarationrs in either position. Some people are quite vocal about preferring
the latter, but I can't get worked up over 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/02/25 Raw View
In article <1e6h706.5djw5n1vv4q2dN%firstian@nospam.bellatlantic.net>,
Joe Chan <firstian@nospam.bellatlantic.net> writes
>As far as the grammar goes, const char and char const are the same. I
>personally find the first form more readable, and have seldom seen the
>latter.
Then you have missed reading excellent material from Dan Saks:) Both C
and C++ allow really weird orderings for declarators but that is no
reason for doing so. If you never start a declaration with const (or
volatile) you never have to worry about the meaning of:
const MYPOINTERTYPE;
(think about the difference between #define and typedef used to define
MYPOINTERTYPE)
Francis Glassborow Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 2000/02/25 Raw View
Paulo Pinto wrote:
>
> Is there any change in the standard way to place const in function
> arguments ?
>
> For example these declarations
>
> void func1 (const char *)
> void func2 (const xpto &)
>
> have the same meaning as
>
> void func1 (char const *)
> void func2 (xpto const &)
>
> Since I started to use C and C++ (depending on the project) about 10
> years ago and I have seen almost everyone using the first form. Now
> there is
> an increasing number of code samples that use the second form.
>
> Is this second form, now the official way to declare arguments that
> are const ?
Both ways are equally official.
The point in putting const to the right is consisteny
(this way, const _always_ modifies the item on the left)
and less confusion (how often have you seen postings
asking why "typedef char* pchar; const pchar foo;" is
not the same as "const char* foo;"? If you consistently
put the const to the right, going from "pchar const foo;"
to "char* const foo" is a no-brainer).
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Michel Michaud <micm19@gest-netware.cstjean.qc.ca>
Date: 2000/03/07 Raw View
Christopher Eltschka wrote :
> The point in putting const to the right is consisteny
> (this way, const _always_ modifies the item on the left)
> and less confusion (how often have you seen postings
> asking why "typedef char* pchar; const pchar foo;" is
> not the same as "const char* foo;"? If you consistently
> put the const to the right, going from "pchar const foo;"
> to "char* const foo" is a no-brainer).
Just as an aside, I used to put const to the right (I
started that as soon as ANSI-C introduced const). As
I'm a teacher I found it better that way of course. But
my students kept having problem when they read books
that used the other style... that was all books! So now
I teach the rules carefully, present both styles,
and use the "sometime to the left" style because it
is consistent with what most books still use, some
of them from very very very well known and respected
authors :-). But I do think they would understand
the other style because they know the rules...
--
Michel Michaud (micm19@removethis.mail2.cstjean.qc.ca)
http://www3.sympatico.ca/michel.michaud
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Rob Stewart <donotspamme@giage.com>
Date: 2000/03/08 Raw View
Michel Michaud wrote:
>
> Christopher Eltschka wrote :
> > The point in putting const to the right is consisteny
> > (this way, const _always_ modifies the item on the left)
> > and less confusion (how often have you seen postings
> > asking why "typedef char* pchar; const pchar foo;" is
> > not the same as "const char* foo;"? If you consistently
> > put the const to the right, going from "pchar const foo;"
> > to "char* const foo" is a no-brainer).
>
> Just as an aside, I used to put const to the right (I
> started that as soon as ANSI-C introduced const). As
> I'm a teacher I found it better that way of course. But
> my students kept having problem when they read books
> that used the other style... that was all books! So now
> I teach the rules carefully, present both styles,
> and use the "sometime to the left" style because it
> is consistent with what most books still use, some
> of them from very very very well known and respected
> authors :-). But I do think they would understand
> the other style because they know the rules...
As a teacher, wouldn't you want to teach them the best techniques
you can? If you agree that putting const on the right eliminates
a source of errors, you should tell them so and encourage them to
do it that way. When I read books with const on the left (which
is still most of them, unfortunately), I just think "how
unfortunate" or something, but I can read it just fine.
If your students are regularly exposed to both styles, they'll
learn to read both easily and won't be confused. You might start
with it on the left initially, then explain the alternative and
transition to doing it on the right. That will allow them to
make the transition with you while remaining accustomed to the
other style so reading books won't confuse them.
--
Robert Stewart | rob-at-giage-dot-com
Software Engineer | using std::disclaimer;
Giage, Ltd. | http://www.giage.com
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Michel Michaud <micm19@gest-netware.cstjean.qc.ca>
Date: 2000/03/08 Raw View
Rob Stewart a crit :
> As a teacher, wouldn't you want to teach them the best techniques
> you can? If you agree that putting const on the right eliminates
> a source of errors, you should tell them so and encourage them to
> do it that way. When I read books with const on the left (which
> is still most of them, unfortunately), I just think "how
> unfortunate" or something, but I can read it just fine.
The other "problem" is one of habits. As I see it, most
companies that have programming guidelines don't put the
const on the right. I want my student to fit in as easily
as possible. They could easily switch style if asked, but
the odds that they would have to adopt the "left style" is
higher, I go with the odds, for good or for worse.
But I would immediatly reconsider if there was one
"authorative" book that would start using the left
style. Who of Stroustrup, Meyers, Koenig, Lippman,
Sutter, Coplien, etc. will take up the challenge?
--
Michel Michaud (micm19@removethis.mail2.cstjean.qc.ca)
http://www3.sympatico.ca/michel.michaud
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: David R Tribble <david@tribble.com>
Date: 2000/03/08 Raw View
Christopher Eltschka wrote :
>> > The point in putting const to the right is consisteny
>> > (this way, const _always_ modifies the item on the left)
>> > and less confusion (how often have you seen postings
>> > asking why "typedef char* pchar; const pchar foo;" is
>> > not the same as "const char* foo;"? If you consistently
>> > put the const to the right, going from "pchar const foo;"
>> > to "char* const foo" is a no-brainer).
Rob Stewart wrote:
>> Just as an aside, I used to put const to the right (I
>> started that as soon as ANSI-C introduced const). As
>> I'm a teacher I found it better that way of course. But
>> my students kept having problem when they read books
>> that used the other style... that was all books! So now
>> I teach the rules carefully, present both styles,
>> and use the "sometime to the left" style because it
>> is consistent with what most books still use, some
>> of them from very very very well known and respected
>> authors :-). But I do think they would understand
>> the other style because they know the rules...
Rob Stewart wrote:
> As a teacher, wouldn't you want to teach them the best techniques
> you can? If you agree that putting const on the right eliminates
> a source of errors, you should tell them so and encourage them to
> do it that way. When I read books with const on the left (which
> is still most of them, unfortunately), I just think "how
> unfortunate" or something, but I can read it just fine.
>
> If your students are regularly exposed to both styles, they'll
> learn to read both easily and won't be confused. You might start
> with it on the left initially, then explain the alternative and
> transition to doing it on the right. That will allow them to
> make the transition with you while remaining accustomed to the
> other style so reading books won't confuse them.
So how do you explain how to place 'mutable'?
-- David R. Tribble, david@tribble.com, http://david.tribble.com --
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Rob Stewart <donotspamme@giage.com>
Date: 2000/03/08 Raw View
David R Tribble wrote:
>
> Rob Stewart wrote:
> > As a teacher, wouldn't you want to teach them the best techniques
> > you can? If you agree that putting const on the right eliminates
> > a source of errors, you should tell them so and encourage them to
> > do it that way. When I read books with const on the left (which
> > is still most of them, unfortunately), I just think "how
> > unfortunate" or something, but I can read it just fine.
> >
> > If your students are regularly exposed to both styles, they'll
> > learn to read both easily and won't be confused. You might start
> > with it on the left initially, then explain the alternative and
> > transition to doing it on the right. That will allow them to
> > make the transition with you while remaining accustomed to the
> > other style so reading books won't confuse them.
>
> So how do you explain how to place 'mutable'?
mutable is a storage-class-specifier, so it is different.
mutable can only be applied to a dm (unless I missed something),
so its use is highly specialized. OTOH, const can be applied to
mfs, dms and other variables, parameters, and typedefs (or rather
their use in a declaration); did I miss something? There is only
one use -- as part of a type-specifier -- in which const may be
on the left or right.
Consequently, I always put mutable on the left and would teach
students the same. Do you see any inconsistency?
--
Robert Stewart | rob-at-giage-dot-com
Software Engineer | using std::disclaimer;
Giage, Ltd. | http://www.giage.com
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: 2000/03/08 Raw View
Michel Michaud wrote:
>
>
> But I would immediatly reconsider if there was one
> "authorative" book that would start using the left
> style. Who of Stroustrup, Meyers, Koenig, Lippman,
> Sutter, Coplien, etc. will take up the challenge?
>
Stroustrup puts it on the left. Frankly, as I've said before, I can't
get worked up over it. You need to know that they can go in either
order. Frankly, even though it's not my pick of styles, I recommend
that if nothing else people use Stroustrup's style. At least most
of us C++ gurus are comfortable reading 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Paulo Pinto <paulo.pinto@intervento.com>
Date: 2000/02/24 Raw View
Is there any change in the standard way to place const in function
arguments ?
For example these declarations
void func1 (const char *)
void func2 (const xpto &)
have the same meaning as
void func1 (char const *)
void func2 (xpto const &)
Since I started to use C and C++ (depending on the project) about 10
years ago and I have seen almost everyone using the first form. Now
there is
an increasing number of code samples that use the second form.
Is this second form, now the official way to declare arguments that
are const ?
Thanks in advance
--
-----------------------------------------------------------------------
| Heaven doesn 't want me | Use Linux. |
| & Hell is afraid I'll take over. | The choice of a free generation. |
-----------------------------------------------------------------------
| Paulo Pinto, pjmlp@students.si.fct.unl.pt |
| http://students.fct.unl.pt/users/pjmlp |
---------------------------------------------
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: firstian@nospam.bellatlantic.net (Joe Chan)
Date: 2000/02/24 Raw View
As far as the grammar goes, const char and char const are the same. I
personally find the first form more readable, and have seldom seen the
latter.
Paulo Pinto <paulo.pinto@intervento.com> wrote:
> Is there any change in the standard way to place const in function
> arguments ?
>
> For example these declarations
>
> void func1 (const char *)
> void func2 (const xpto &)
>
> have the same meaning as
>
> void func1 (char const *)
> void func2 (xpto const &)
>
> Since I started to use C and C++ (depending on the project) about 10
> years ago and I have seen almost everyone using the first form. Now
> there is
> an increasing number of code samples that use the second form.
>
> Is this second form, now the official way to declare arguments that
> are const ?
>
> Thanks in advance
--
Joe Chan
Remove "nospam" to get my address.
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 2000/02/25 Raw View
Paulo Pinto wrote:
>
> Is there any change in the standard way to place const in function
> arguments ?
>
> For example these declarations
>
> void func1 (const char *)
> void func2 (const xpto &)
>
> have the same meaning as
>
> void func1 (char const *)
> void func2 (xpto const &)
>
> Since I started to use C and C++ (depending on the project) about 10
> years ago and I have seen almost everyone using the first form. Now
> there is
> an increasing number of code samples that use the second form.
>
> Is this second form, now the official way to declare arguments that
> are const ?
They're both still legal, and of equal legitimacy. I recently concluded
that there's a minor advantage favoring the second form. Consider:
#define CHAR_PTR char *
typedef char *char_ptr;
const CHAR_PTR p1;
CHAR_PTR const p2;
const char_ptr p3;
char_ptr const p4;
const char *p5;
char const *p6;
char * const p7;
p1, p5, and p6 are all of the same type. p2, p3, p4, and p7 are the same
type.
Novice programmers who are sophisticated enough to correctly understand
p5, p6, and p7, are still often suprised that p1 and p2 are different
types, while p3 and p4 are the same type. That's because they tend to
think of typedefs as being the same as macros. Therefore, it's slightly
safer to avoid confusing the novices, by using the form of p4. That way,
misinterpreting it as a macro produces the same result as interpreting
it correctly. For consistency's sake, you should do the same with all
types, not just the ones that are typedefs. You might reach a different
conclusion if you don't work with as many novice programmers as I do.
However, I only recent reached this conclusion, and most of my code uses
the style of p3.
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: jpotter@falcon.lhup.edu (John Potter)
Date: 2000/02/25 Raw View
On Thu, 24 Feb 2000 11:21:04 CST, Paulo Pinto
<paulo.pinto@intervento.com> wrote:
: Is there any change in the standard way to place const in function
: arguments ?
No, just a matter of style/taste.
: For example these declarations
:
: void func1 (const char *)
: void func2 (const xpto &)
:
: have the same meaning as
:
: void func1 (char const *)
: void func2 (xpto const &)
The arguement goes: Const modifies the thing to its left except when
there is nothing to its left and it must modify the thing to its right.
Read right to left.
char**const constant pointer to mutable pointer to mutable char
char*const* mutable pointer to constant pointer to mutable char
char const** mutable pointer to mutable pointer to constant char
const char** mutable pointer to mutable pointer to charable const
typedef char* charPtr
charPtr const obviously a constant pointer to mutable char
const charPtr it's not a mutable pointer to constant char
: Since I started to use C and C++ (depending on the project) about 10
: years ago and I have seen almost everyone using the first form. Now
: there is
: an increasing number of code samples that use the second form.
Maybe things are changing. I'm sure you will see both forms for a
very long time. Chew on it. If it tastes good, use it. :)
John
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Martin Abernethy <Martin.Abernethy@liffe.com>
Date: 2000/02/25 Raw View
Paulo Pinto wrote in message <38B3C3EB.7D9B1192@intervento.com
<mailto:38B3C3EB.7D9B1192@intervento.com> >...>
> Is there any change in the standard way to place const in function
>arguments ?
>
> For example these declarations
>
> void func1 (const char *)
> void func2 (const xpto &)
>
> have the same meaning as
>
> void func1 (char const *)
> void func2 (xpto const &)
>
> Since I started to use C and C++ (depending on the project) about 10
>years ago and I have seen almost everyone using the first form. Now
>there is
>an increasing number of code samples that use the second form.
>
> Is this second form, now the official way to declare arguments that
>are const ?
I have always preferred the second way as being more regular - the const
qualifier immediately follows the item it is qualifying, just like in const
member functions.
I think the second form is becoming more popular because it results in fewer
surprises when using templates. For example:
template <class T>
class example
{
public:
const T func1(const T x) const;
T const func2(T const x) const;
};
func1 and func2 have identical signatures. However, if example<T> is
instantiated with the type char*, it looks as if func1 ought to take a
parameter of type 'const char*' , and func2 a parameter type of 'char*
const', which are different. The correct parameter type is 'char* const',
which is more obvious when the second form is used.
Martin Abernethy
----------------------------------------------------------------------
The information contained in this e-mail is confidential and solely for the intended addressee(s). Unauthorised reproduction, disclosure, modification, and/or distribution of this email may be unlawful. If you have received this email in error, please notify the sender immediately and delete it from your system. The views expressed in this message do not necessarily reflect those of LIFFE (Holdings) Plc or any of its subsidiary companies.
----------------------------------------------------------------------
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]