C++ Logo

std-proposals

Advanced search

Re: Simple Beautification and readability open for template classes

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Fri, 16 Oct 2020 00:48:11 -0400
On Thu, Oct 15, 2020 at 9:38 PM Chris Green via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
>
> I have been working on a set of tools and settings for improving the readability, documentability, and aesthetics of c++, mainly through IDE efforts. You can get pretty far with current existing tools and a few good macros and conventions.
>
> Looking at the readability of header files defining template classes (for instance, std::), It's clear to me that the template declarations are really hindering readability. Not just because of their syntax, but they cause there to be SO MUCH text between the start of the declaration and the thing you are declaring. Here is an example from microsoft's implementation of std::span from github. I picked std::span because conceptually it should be fairly simple.
> Here is the actual class declaration, viewed as plain text:
>
> template <class _Ty, size_t _Extent = dynamic_extent> class span : private _Span_extent_type<_Ty, _Extent> {

Why did you write it that way? One of the few nearly universal
formatting rules for C++ is that template headers go on a separate
line from the thing they define. Microsoft certainly didn't; I just
checked their STL repository, and "class span" is on a separate line
the way it normally appears. I'm not sure I've seen C++ code that
commonly puts template definitions in one line like this. Oh, it might
be done for a quickie variable template or alias template. But that's
about it.

Once you move `class span` to where it belongs, you can easily tell
that it's declaring a class template. Basically, this is a problem we
solved probably around the time templates were invented.

> It's basically impossible to tell at a glance what that is even declaring (the 'span' class). Loading it into an editor with colorization doesn't make it any better, maybe even worse:
>
>
>
> My proposal is that the abbreviated syntax for defining template functions without 'template' be extended to classes. The 'class' declaration statement shall allow a templated argument list enclosed in angle brackets immediately after the name of the new identifier.

So what happens if it's not a "new" identifier? Can you not forward
declare template classes with this syntax? How do you go about writing
specializations of templates with this syntax?

> This shall be equivalent in all ways to adding the "template" keyword before the definition, with the chosen arguments.
>
>
> class span<class _ElementType, size_t+ _Extent = dynamic_extent> : private pan_extent_type<_ElementType, _Extent>

So aside from putting the `class` bit first, you save... the 8 letters
it takes to write the word "template". Abbreviated function syntax at
least has the benefit of being materially *shorter* than using the
full template header in most cases.

I don't think C++ needs to add more ways to say the same thing. The
current way, when written properly, is adequate. This change doesn't
seem worth the effort.

Received on 2020-10-15 23:48:20