C++ Logo

std-proposals

Advanced search

Re: Down with "template"! (up with the turbofish)

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sat, 9 Oct 2021 19:21:14 -0400
On Sat, Oct 9, 2021 at 1:20 PM Jean-Baptiste Vallon Hoarau via
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> Jason :
>>
>> I believe his point is that, even though it's "simpler" than
>> `template`, you still have to do *something* that you wouldn't
>> ordinarily have to do. That is, you still have to know that you need
>> disambiguation syntax. As such, your proposal doesn't fix the C++
>> parser to take the expected code. It merely provides a different
>> spelling of the disambiguated code.
>
>
> If I could magically make the C++ grammar context-free, I would. However it's too late for that, the grammar is undecidable and having to use a disambiguator is the price to pay in some cases. The question is what kind of disambiguator do we want : one so bad that everyone puts extra work to avoid needing it, or one that's reasonably usable and can be used coherently in non ambiguous code.

You're going to have to put forth some pretty compelling evidence to
convince people that "everyone puts extra work to avoid needing"
`template`. Remember: most template functions deduce their parameters
*naturally* (ie: without having to write them to do so). So the number
of functions in question is pretty small.

And no, nobody's going to use `::<` unless they *have to*.

>> The problem you're talking about is one that the compiler basically
>> tells you how to fix. "It looks like you tried to write a template
>> specialization, but you need to put 'template' there." You encountered
>> a problem and understand what you need to do to solve it.
>
>
> I think there's a misunderstanding about the nature of the problem. The problem is not that people try to write this kind of code and it fails : this happens once. The problem is that people now have to go out of their way to workaround something so basic, which happens everyday. How many times do programmers accidentally step on the "most vexing parse"? Perhaps, once or twice before they know what's going on. The fix is easy : use "{}" or "=" to declare and initialize variables. You can use that and forget about it because the solution is unobtrusive. You can't forget about having to type ".template" everywhere or turns everything into a deducible parameters : it's both ugly and requires cognitive load, that you could have used to do more interesting things than fixing the C++ grammar.

It requires no more "cognitive load" than `::<`, since people are only
going to use that where they have to.

>> If having to write 9 extra characters
>> is enough to make you fundamentally rewrite code, I don't see how
>> having to write 2 characters would make you stop.
>
> Isn't 2 a lot less than 9? I have no trouble seeing how not interspersing my code with a 9 letter word when most of my identifiers are less than that, and replacing that with a 2 characters punctuator would improve readability.
>
>> C++ didn't make
>> `std::get` a free function because making it a template member of
>> `tuple` would make it too ugly to call. We did it because it creates a
>> uniform interface for extracting members which other types (like
>> `pair`) can participate in.
>
> How would this have been impossible with a member function?

Let me finish that sentence more directly: "which other types can
participate in *without* having to modify the definition of those
types". Also see `begin/end`, `size`, `data`, `swap`, and so forth.

Extensible uniform APIs are best built from free functions in C++, not
member functions. Or at least, not *only* member functions.

Received on 2021-10-09 18:21:27