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

"Is C++ type-safe? (There’s two right answers)" by davmac

36 views
Skip to first unread message

Lynn McGuire

unread,
Dec 14, 2020, 7:39:01 PM12/14/20
to
"Is C++ type-safe? (There’s two right answers)" by davmac

https://davmac.wordpress.com/2020/12/13/is-c-type-safe-theres-two-right-answers/

Yup, he is right. And that is why C++ works. And we have wayyyyy too
many type casts in our software.

Lynn

Melzzzzz

unread,
Dec 14, 2020, 8:05:39 PM12/14/20
to
Language without undefined behavior would be useless for low level
programming. Speaking of safety Rust vs C++ is it better not to have
pointer arithmetic and do that by converting pointer to int and back and
C++ model in which you just add value to pointer?
>
> Lynn


--
current job title: senior software engineer
skills: c++,c,rust,go,nim,haskell...

press any key to continue or any other to quit...
U ničemu ja ne uživam kao u svom statusu INVALIDA -- Zli Zec
Svi smo svedoci - oko 3 godine intenzivne propagande je dovoljno da jedan narod poludi -- Zli Zec
Na divljem zapadu i nije bilo tako puno nasilja, upravo zato jer su svi
bili naoruzani. -- Mladen Gogala

Öö Tiib

unread,
Dec 15, 2020, 8:36:57 AM12/15/20
to
On Tuesday, 15 December 2020 at 03:05:39 UTC+2, Melzzzzz wrote:
> On 2020-12-15, Lynn McGuire <lynnmc...@gmail.com> wrote:
> > "Is C++ type-safe? (There’s two right answers)" by davmac
> >
> > https://davmac.wordpress.com/2020/12/13/is-c-type-safe-theres-two-right-answers/
> >
> > Yup, he is right. And that is why C++ works. And we have wayyyyy too
> > many type casts in our software.
> Language without undefined behavior would be useless for low level
> programming. Speaking of safety Rust vs C++ is it better not to have
> pointer arithmetic and do that by converting pointer to int and back and
> C++ model in which you just add value to pointer?

The unsafe operations are useful only in limited occasions and
in limited meanings even in low level embedded code.
So it is anyway good idea to make unsafe things like pointer
arithmetic or type punning to be ugly and inconvenient to express.
Totally different question is if need of type punning a pointer into
integral value and back is best uglifier factor but it works.
Bad code that does naked casts and pointer arithmetic all
over the place will look like garbage it is and so mission
accomplished.

spu...@isnotyourbuddy.co.uk

unread,
Dec 15, 2020, 9:11:46 AM12/15/20
to
On Tue, 15 Dec 2020 05:36:40 -0800 (PST)
=?UTF-8?B?w5bDtiBUaWli?= <oot...@hot.ee> wrote:
>On Tuesday, 15 December 2020 at 03:05:39 UTC+2, Melzzzzz wrote:
>> On 2020-12-15, Lynn McGuire <lynnmc...@gmail.com> wrote:=20
>> > "Is C++ type-safe? (There=E2=80=99s two right answers)" by davmac=20
>> >=20
>> > https://davmac.wordpress.com/2020/12/13/is-c-type-safe-theres-two-right=
>-answers/=20
>> >=20
>> > Yup, he is right. And that is why C++ works. And we have wayyyyy too=20
>> > many type casts in our software.
>> Language without undefined behavior would be useless for low level=20
>> programming. Speaking of safety Rust vs C++ is it better not to have=20
>> pointer arithmetic and do that by converting pointer to int and back and=
>=20
>> C++ model in which you just add value to pointer?=20
>
>The unsafe operations are useful only in limited occasions and
>in limited meanings even in low level embedded code.

But when needed they're REALLY needed.

>So it is anyway good idea to make unsafe things like pointer
>arithmetic or type punning to be ugly and inconvenient to express.

How is pointer arithmetic inconvenient to express? It couldn't be any simpler.

>Totally different question is if need of type punning a pointer into
>integral value and back is best uglifier factor but it works.=20

Oh yeah, that would really make the code clearer.

>Bad code that does naked casts and pointer arithmetic all
>over the place will look like garbage it is and so mission
>accomplished.=20

I've found that people who have a problem with pointer arithmetic don't
really understand it. Or pointers.

Öö Tiib

unread,
Dec 15, 2020, 10:30:11 AM12/15/20
to
On Tuesday, 15 December 2020 at 16:11:46 UTC+2, spu...@isnotyourbuddy.co.uk wrote:
> On Tue, 15 Dec 2020 05:36:40 -0800 (PST)
> =?UTF-8?B?w5bDtiBUaWli?= <oot...@hot.ee> wrote:
> >On Tuesday, 15 December 2020 at 03:05:39 UTC+2, Melzzzzz wrote:
> >> On 2020-12-15, Lynn McGuire <lynnmc...@gmail.com> wrote:=20
> >> > "Is C++ type-safe? (There=E2=80=99s two right answers)" by davmac=20
> >> >=20
> >> > https://davmac.wordpress.com/2020/12/13/is-c-type-safe-theres-two-right=
> >-answers/=20
> >> >=20
> >> > Yup, he is right. And that is why C++ works. And we have wayyyyy too=20
> >> > many type casts in our software.
> >> Language without undefined behavior would be useless for low level=20
> >> programming. Speaking of safety Rust vs C++ is it better not to have=20
> >> pointer arithmetic and do that by converting pointer to int and back and=
> >=20
> >> C++ model in which you just add value to pointer?=20
> >
> >The unsafe operations are useful only in limited occasions and
> >in limited meanings even in low level embedded code.
> But when needed they're REALLY needed.

So these are in Rust.

> >So it is anyway good idea to make unsafe things like pointer
> >arithmetic or type punning to be ugly and inconvenient to express.
> How is pointer arithmetic inconvenient to express? It couldn't be any simpler.

My point was that it is bad when rarely needed unsafe operations are
difficult to notice.

> >Totally different question is if need of type punning a pointer into
> >integral value and back is best uglifier factor but it works.=20
>
> Oh yeah, that would really make the code clearer.

Yes, the unsafe stuff stands out so it is more clear that it is unsafe.

> >Bad code that does naked casts and pointer arithmetic all
> >over the place will look like garbage it is and so mission
> >accomplished.=20
>
> I've found that people who have a problem with pointer arithmetic don't
> really understand it. Or pointers.

That is unfortunately so with every rarely needed feature. People
can program years in C++ without doing any pointer arithmetic
ever and so quite many can make errors in it. It saves my time when
I find problematic places quicker.


spu...@isnotyourbuddy.co.uk

unread,
Dec 15, 2020, 11:25:43 AM12/15/20
to
On Tue, 15 Dec 2020 07:29:49 -0800 (PST)
=?UTF-8?B?w5bDtiBUaWli?= <oot...@hot.ee> wrote:
>On Tuesday, 15 December 2020 at 16:11:46 UTC+2, spu...@isnotyourbuddy.co.uk
>wrote:
>> >Totally different question is if need of type punning a pointer into
>> >integral value and back is best uglifier factor but it works.=20
>>
>> Oh yeah, that would really make the code clearer.
>
>Yes, the unsafe stuff stands out so it is more clear that it is unsafe.

Illogical reasoning. Any experience developer knows to be careful with
pointers, they don't need it signposted.

>> I've found that people who have a problem with pointer arithmetic don't
>> really understand it. Or pointers.
>
>That is unfortunately so with every rarely needed feature. People
>can program years in C++ without doing any pointer arithmetic
>ever and so quite many can make errors in it. It saves my time when
>I find problematic places quicker.

Sure, you can program in C++ without ever using pointers if you only ever
program baby code. But if you ever do for example any network packet processing
or are writing a parser, using shared memory or to-the-metal you'll soon get
sick of trying to do it all with std::string/array or some other higher level
abstraction not to mention how slow they'd be. If you only want to use high
level constructs perhaps you'd be better off with Java or Python.

Öö Tiib

unread,
Dec 15, 2020, 1:52:46 PM12/15/20
to
On Tuesday, 15 December 2020 at 18:25:43 UTC+2, spu...@isnotyourbuddy.co.uk wrote:
> On Tue, 15 Dec 2020 07:29:49 -0800 (PST)
> =?UTF-8?B?w5bDtiBUaWli?= <oot...@hot.ee> wrote:
> >On Tuesday, 15 December 2020 at 16:11:46 UTC+2, spu...@isnotyourbuddy.co.uk
> >wrote:
> >> >Totally different question is if need of type punning a pointer into
> >> >integral value and back is best uglifier factor but it works.=20
> >>
> >> Oh yeah, that would really make the code clearer.
> >
> >Yes, the unsafe stuff stands out so it is more clear that it is unsafe.
> Illogical reasoning. Any experience developer knows to be careful with
> pointers, they don't need it signposted.

I do not really care how well they know their own code. For me it is important
that they can be used to work with code of other people.

> >> I've found that people who have a problem with pointer arithmetic don't
> >> really understand it. Or pointers.
> >
> >That is unfortunately so with every rarely needed feature. People
> >can program years in C++ without doing any pointer arithmetic
> >ever and so quite many can make errors in it. It saves my time when
> >I find problematic places quicker.
> Sure, you can program in C++ without ever using pointers if you only ever
> program baby code.

That is for what is paid. It is tricky to find those rare customers who
let you waste $25K and whole month for writing a parser or video
codec or compression algorithm. Use best existing one. But interface
of such does not involve pointer arithmetic.

> But if you ever do for example any network packet processing
> or are writing a parser, using shared memory or to-the-metal you'll soon get
> sick of trying to do it all with std::string/array or some other higher level
> abstraction not to mention how slow they'd be. If you only want to use high
> level constructs perhaps you'd be better off with Java or Python.

Yes I'm quite bad with Python. In my company we have a little
homework assignment ... about processing text file. We sometimes
hire people whose C++ performs even weaker than my Python script
but we avoid those whose garbage works incorrectly.



0 new messages