Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: const T vs T const

112 views
Skip to first unread message

Paavo Helde

unread,
Sep 26, 2016, 2:56:28 PM9/26/16
to
On 26.09.2016 20:50, Stefan Ram wrote:
> It was possibly in this newsgroup where the topic
> "const T vs T const" was discussed some months ago.
> (Or it might have been "comp.lang.c".)
>
> Dan Sacks famously wrote about it and recommended
> "T const" IIRC.
>
> I now found something new about this topic in the
> "Library Design Guidlines" for C++: quote
>
> const goes in the wrong place, i.e., to the left
>
> unquote. Thus, they acknowledge what Dan Sacks
> wrote by using "wrong", only to then recommend
> the opposite, i.e., "left".
>
> Why? Maybe because tradition dictates this. (No
> rationale is given there.)


Yes, "T const" would be more regular. However, lots of people prefer
"const T". I guess this is just because when you talk about weather in
English then "constant rain" often makes more sense than "rain constant".

Cheers
Paavo

Richard

unread,
Sep 26, 2016, 3:04:19 PM9/26/16
to
[Please do not mail me a copy of your followup]

Paavo Helde <myfir...@osa.pri.ee> spake the secret code
<WuSdnSoppZPM8nTK...@giganews.com> thusly:

>Yes, "T const" would be more regular. However, lots of people prefer
>"const T".

In interviews I ask people to write the declaration for a constant
pointer to constant character. When they've only ever seen const on
the left, they freeze up.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

Paavo Helde

unread,
Sep 26, 2016, 3:10:59 PM9/26/16
to
On 26.09.2016 22:04, Richard wrote:
> [Please do not mail me a copy of your followup]
>
> Paavo Helde <myfir...@osa.pri.ee> spake the secret code
> <WuSdnSoppZPM8nTK...@giganews.com> thusly:
>
>> Yes, "T const" would be more regular. However, lots of people prefer
>> "const T".
>
> In interviews I ask people to write the declaration for a constant
> pointer to constant character. When they've only ever seen const on
> the left, they freeze up.

And if they have only ever seen 'char const *p', do they not freeze up?
Just curious...



Wouter van Ooijen

unread,
Sep 26, 2016, 3:16:51 PM9/26/16
to
Op 26-Sep-16 om 9:04 PM schreef Richard:
> [Please do not mail me a copy of your followup]
>
> Paavo Helde <myfir...@osa.pri.ee> spake the secret code
> <WuSdnSoppZPM8nTK...@giganews.com> thusly:
>
>> Yes, "T const" would be more regular. However, lots of people prefer
>> "const T".
>
> In interviews I ask people to write the declaration for a constant
> pointer to constant character. When they've only ever seen const on
> the left, they freeze up.

I would refuse to write that as one declaration.

Wouter "Objects? No Thanks!" van Ooijen

Chris M. Thomasson

unread,
Sep 26, 2016, 3:21:56 PM9/26/16
to
IMVHO, they really shouldn't. I personally write these things from right
to left, so a constant pointer to a constant character would be:

char const* const

Read right to left, it is const *, to const char.

IMHO, its pretty straightforward.

Bo Persson

unread,
Sep 26, 2016, 4:16:14 PM9/26/16
to
On 2016-09-26 19:50, Stefan Ram wrote:
> It was possibly in this newsgroup where the topic
> "const T vs T const" was discussed some months ago.
> (Or it might have been "comp.lang.c".)
>
> Dan Sacks famously wrote about it and recommended
> "T const" IIRC.
>
> I now found something new about this topic in the
> "Library Design Guidlines" for C++: quote
>
> const goes in the wrong place, i.e., to the left
>
> unquote. Thus, they acknowledge what Dan Sacks
> wrote by using "wrong", only to then recommend
> the opposite, i.e., "left".
>
> Why? Maybe because tradition dictates this. (No
> rationale is given there.)
>

Of course there is no rationale, so we have to pick one order.
Apparently Bjarne has always preferred "const int i = 0;".


And I have never seen anyone arguing that

constexpr int x = 0;

is the wrong order. So why have different rules for const and constexpr?


Bo Persson





Alf P. Steinbach

unread,
Sep 26, 2016, 6:43:16 PM9/26/16
to
On 26.09.2016 19:50, Stefan Ram wrote:
> It was possibly in this newsgroup where the topic
> "const T vs T const" was discussed some months ago.
> (Or it might have been "comp.lang.c".)
>
> Dan Sacks famously wrote about it and recommended
> "T const" IIRC.

In order to write `const` first, simply define

template< class Type >
using Ptr_ = Type*;

Now write e.g.

Ptr_<const int> p;

and

const Ptr_<int> p;

Easy peasy. :)

Well except for `auto` type deduction, there's a kind of black hole in
the language there, but one can live with that.



> I now found something new about this topic in the
> "Library Design Guidlines" for C++: quote
>
> const goes in the wrong place, i.e., to the left
>
> unquote. Thus, they acknowledge what Dan Sacks
> wrote by using "wrong", only to then recommend
> the opposite, i.e., "left".
>
> Why? Maybe because tradition dictates this. (No
> rationale is given there.)

Not sure exactly what you're quoting here or what you're asking for
rationale about, can you clarify?


Cheers!,

- Alf

Richard

unread,
Sep 26, 2016, 7:15:59 PM9/26/16
to
[Please do not mail me a copy of your followup]

Paavo Helde <myfir...@osa.pri.ee> spake the secret code
<qpCdnW-QCfwl73TK...@giganews.com> thusly:
I don't know. Noone I've interviewed has ever been able to answer
the question without help.

Richard

unread,
Sep 26, 2016, 7:16:41 PM9/26/16
to
[Please do not mail me a copy of your followup]

(Richard) legaliz...@mail.xmission.com spake the secret code
<nsca75$um0$1...@news.xmission.com> thusly:

>[Please do not mail me a copy of your followup]
>
>Paavo Helde <myfir...@osa.pri.ee> spake the secret code
><qpCdnW-QCfwl73TK...@giganews.com> thusly:
>
>>On 26.09.2016 22:04, Richard wrote:
>>> [Please do not mail me a copy of your followup]
>>>
>>> Paavo Helde <myfir...@osa.pri.ee> spake the secret code
>>> <WuSdnSoppZPM8nTK...@giganews.com> thusly:
>>>
>>>> Yes, "T const" would be more regular. However, lots of people prefer
>>>> "const T".
>>>
>>> In interviews I ask people to write the declaration for a constant
>>> pointer to constant character. When they've only ever seen const on
>>> the left, they freeze up.
>>
>>And if they have only ever seen 'char const *p', do they not freeze up?
>>Just curious...
>
>I don't know. Noone I've interviewed has ever been able to answer
>the question without help.

...and all their attempts had const on the left until they tried to
figure out how to express the idea that the pointer was const and
then they failed.

Alf P. Steinbach

unread,
Sep 26, 2016, 11:32:43 PM9/26/16
to
On 27.09.2016 01:34, Stefan Ram wrote:
> "Alf P. Steinbach" <alf.p.stein...@gmail.com> writes:
>> Not sure exactly what you're quoting here or what you're asking for
>> rationale about, can you clarify?
>
> The rationale for the apparent guideline to write
>
> const int i = 2;
>
> instead of
>
> int const i = 2;
>
> . I was quoting the Library Design Guidelines
> (Standard Library Guidelines) from isocpp.org.
>

Oh, I understand. Sorry.

Well, standard library work has to follow the conventions already
established for the C++ standard. It would be just too much work to
change those conventions now. So that's a very direct rationale.

I read “the wrong place” as an acknowledgment that the original choice,
back in the 1990's, was sub-optimal.

But, in modern C++ we don't have to cater to the idiosyncracies of the C
declaration syntax.

We can just define a `Ptr_` type builder, and a `Ref_`, and so on.

• • •

At first, years ago, I went all the way and also defined `Const_`.

But it's silly to define a new syntax where the current one works.

I found it useful to define a `With_const_like_`, in order to transfer
constness from one type to another. But I'm still searching for a
better, shorter name for that.

• • •

There is also a more radical idea of defining `const` versions of all
the basic types and require use of e.g. `Var_` to make something
mutable. I don't think this idea can be made practically workable: the
confusion would be akin to redefining `+` as `-`. But with this idea

Ptr_<Int> p;

would not compile, because it would declare an uninitialized `int const*
const`.

One would have write e.g.

Var_<Ptr_<Int_>> p;

to get something mutable.

Or

Ptr_<Var_<Int>> p = &blah;

:)


Cheers!,

- Alf

Gareth Owen

unread,
Sep 27, 2016, 1:51:31 AM9/27/16
to
r...@zedat.fu-berlin.de (Stefan Ram) writes:

> It was possibly in this newsgroup where the topic
> "const T vs T const" was discussed some months ago.
> (Or it might have been "comp.lang.c".)

As an equally vital followup, how many angels can we fit on the head of
a pin?
0 new messages