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

Best C++20 feature

78 views
Skip to first unread message

Mr Flibble

unread,
Jul 26, 2019, 10:42:21 AM7/26/19
to
By far and away the best C++20 feature is strong typedefs: we have been
waiting so long for this feature and it has finally arrived:

strong typedef int wibble_t;

void foo(int i) {}
void foo(wibble_t w) {} // this overload is called! \o/

wibble_t w = 42;
foo(w);

/Flibble

--
"Snakes didn't evolve, instead talking snakes with legs changed into
snakes." - Rick C. Hodgin

“You won’t burn in hell. But be nice anyway.” – Ricky Gervais

“I see Atheists are fighting and killing each other again, over who
doesn’t believe in any God the most. Oh, no..wait.. that never happens.” –
Ricky Gervais

"Suppose it's all true, and you walk up to the pearly gates, and are
confronted by God," Bryne asked on his show The Meaning of Life. "What
will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery
that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a
world that is so full of injustice and pain. That's what I would say."

Thiago Adams

unread,
Jul 26, 2019, 12:24:14 PM7/26/19
to
On Friday, July 26, 2019 at 11:42:21 AM UTC-3, Mr Flibble wrote:
> By far and away the best C++20 feature is strong typedefs: we have been
> waiting so long for this feature and it has finally arrived:
>
> strong typedef int wibble_t;

Where did you hear about it?
Is this a joke?

Robert Wessel

unread,
Jul 26, 2019, 12:57:39 PM7/26/19
to
On Fri, 26 Jul 2019 15:42:09 +0100, Mr Flibble
<flibbleREM...@i42.co.uk> wrote:

>By far and away the best C++20 feature is strong typedefs: we have been
>waiting so long for this feature and it has finally arrived:
>
>strong typedef int wibble_t;
>
>void foo(int i) {}
>void foo(wibble_t w) {} // this overload is called! \o/
>
>wibble_t w = 42;
>foo(w);


Surely it would have been better to not introduce a new keyword, and
rather add another meaning for static. ;-)

Bonita Montero

unread,
Jul 26, 2019, 1:41:21 PM7/26/19
to
Am 26.07.2019 um 16:42 schrieb Mr Flibble:
> By far and away the best C++20 feature is strong typedefs:
> we have been waiting so long for this feature and it has finally arrived:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I think this is a very special feature and not many have waited
for this.

Keith Thompson

unread,
Jul 26, 2019, 2:19:33 PM7/26/19
to
It's not a joke, but only because jokes are funny.

There is no "strong typedef" feature in C++, and no such feature will be
in C++20.

Apparently Boost has something called BOOST_STRONG_TYPEDEF, which has
some limitations.

There have been proposals for strong typedefs. See, for example,
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3515.pdf

(If I've missed something, and such a feature is proposed for C++20,
please provide a citation.)

(Recall that typedef does not define a new type, just a new name for an
existing type.)

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Thiago Adams

unread,
Jul 26, 2019, 3:24:12 PM7/26/19
to
On Friday, July 26, 2019 at 3:19:33 PM UTC-3, Keith Thompson wrote:
> Thiago Adams writes:
> > On Friday, July 26, 2019 at 11:42:21 AM UTC-3, Mr Flibble wrote:
> >> By far and away the best C++20 feature is strong typedefs: we have been
> >> waiting so long for this feature and it has finally arrived:
> >>
> >> strong typedef int wibble_t;
> >
> > Where did you hear about it?
> > Is this a joke?
>
> It's not a joke, but only because jokes are funny.
>
> There is no "strong typedef" feature in C++, and no such feature will be
> in C++20.
>
> Apparently Boost has something called BOOST_STRONG_TYPEDEF, which has
> some limitations.
>
> There have been proposals for strong typedefs. See, for example,
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3515.pdf
>
> (If I've missed something, and such a feature is proposed for C++20,
> please provide a citation.)
>
> (Recall that typedef does not define a new type, just a new name for an
> existing type.)

I didn't find any c++ proposal for this feature and then I thought
it was a joke.

I think strong-typedefs are useful. In windows HRESULT and BOOL can
be mixed causing errors, for instance.

Some static analysis have this feature implemented:

https://www.gimpel.com/html/strong.htm


Bart

unread,
Jul 26, 2019, 4:22:14 PM7/26/19
to
HRESULT is defined as 'long', an int, so not that much can go wrong.
It's also intended to hold a result, which can be 'bool'.

But it's also misnamed as it is not a handle, which generally are
pointers; those can't be mixed up with bools.

>
> Some static analysis have this feature implemented:
>
> https://www.gimpel.com/html/strong.htm
>
>
That helps explain why Pascal could be such a pain to write. When you
have a million little integer types, all different, and that must match
exactly as well being able to interact, then life is necessarily more
difficult.

It's never that bad in real life: we don't use a different kind of
number to index floors in a building, compared with pages in a book for
example, but how many times does that cause a problem? In code, we can
write:

int page_number, floor_number

to help out.


Thiago Adams

unread,
Jul 26, 2019, 4:34:55 PM7/26/19
to
if I remember correctly the error is something like this:

HRESULT F() { ... return FALSE; }
The value 0 means OK (S_OK) in HRESULT
This code compiles without error or warning.


> But it's also misnamed as it is not a handle, which generally are
> pointers; those can't be mixed up with bools.
>
> >
> > Some static analysis have this feature implemented:
> >
> > https://www.gimpel.com/html/strong.htm
> >
> >
> That helps explain why Pascal could be such a pain to write. When you
> have a million little integer types, all different, and that must match
> exactly as well being able to interact, then life is necessarily more
> difficult.
>
> It's never that bad in real life: we don't use a different kind of
> number to index floors in a building, compared with pages in a book for
> example, but how many times does that cause a problem? In code, we can
> write:
>
> int page_number, floor_number
>
> to help out.

If I had this feature I would not use for each type in the world.
I would not use for kilos or meters (unless maybe doing some
rocket code) because this increases the mental load to understand
the program. If you read 'int meters' you have two good information.
if you read 'Meters meters' you need to known exactly what is Meters.





Mr Flibble

unread,
Jul 26, 2019, 7:03:57 PM7/26/19
to
Great minds think alike: I was going to subsequently reply that my post
would have been more credible if I used "static" instead of "strong" due
to the dearth of contextual meanings for the "static" keyword. :D

Manfred

unread,
Jul 27, 2019, 12:23:22 PM7/27/19
to
On 7/26/2019 10:34 PM, Thiago Adams wrote:
>>> I think strong-typedefs are useful. In windows HRESULT and BOOL can
>>> be mixed causing errors, for instance.
>> HRESULT is defined as 'long', an int, so not that much can go wrong.
>> It's also intended to hold a result, which can be 'bool'.
> if I remember correctly the error is something like this:
>
> HRESULT F() { ... return FALSE; }
> The value 0 means OK (S_OK) in HRESULT
> This code compiles without error or warning.
>
>

And S_FALSE is 1 :)

But VARIANT_TRUE is -1, and VARIANT_FALSE is 0 :))

Expecting consistency is a lost cause in the MS world, so back to the
original point, stuff like strong-typedefs may be useful to enforce
consistency, but not in Windows programming.

David Brown

unread,
Jul 27, 2019, 4:41:49 PM7/27/19
to
Lots and lots can go wrong with a type like that being just a typedef
for an int or a long int.

> It's also intended to hold a result, which can be 'bool'.
>
> But it's also misnamed as it is not a handle, which generally are
> pointers; those can't be mixed up with bools.
>

Handles are sometimes implemented with pointers, sometimes with integer
indexes or other ways of storing data. The point of a handle type is it
should be different from pointers or anything else - it should be more
abstract, and with limited features. You should not be able to
dereference it like a pointer - you should be able to receive it from an
allocation function, pass it around to other API functions, and hand it
in to a deallocation function. Sensible handle types in C are always
strong types - a struct (or even a union) in C, a struct, class or
strong enum in C++.

Juha Nieminen

unread,
Jul 29, 2019, 10:25:22 AM7/29/19
to
Robert Wessel <robert...@yahoo.com> wrote:
> Surely it would have been better to not introduce a new keyword, and
> rather add another meaning for static. ;-)

Well, they used 'class' for strong enums, so "typedef class"?

Alf P. Steinbach

unread,
Jul 29, 2019, 12:00:38 PM7/29/19
to
Sounds nice but that syntax already has a meaning.

On the third hand, `auto` also already had a meaning.


Cheers!,

- Alf

0 new messages