Brainstorming: Explicit instantiation of function template with `auto ref` parameters

Nick Treleaven nick at geany.org
Mon Feb 24 11:33:49 UTC 2025


On Monday, 24 February 2025 at 00:24:03 UTC, Manu wrote:
> Just yet another in the endless stream of cases why ref should 
> be part of
> the type and not a 'storage class'!

Modern languages avoid doing that, just like D. Herb Sutter:

> C++ references were invented to be used as function 
> parameter/return types, and that’s what they’re still primarily 
> useful for. Since C++11, that includes the range-for loop which 
> conceptually works like a function call (see Q&A).

> Sometimes, a reference can also be useful as a local variable, 
> though in modern C++ a pointer or structured binding is usually 
> better (see Q&A).

> That’s it. All other uses of references should be avoided.

https://herbsutter.com/2020/02/23/references-simply/

In particular, the Q & A section:

> For example, if you’re writing a class template, just assume 
> (or document) that it can’t be instantiated with reference 
> types.

Which concludes:

> the dual nature of references is always the problem.

>    If the design embraces the pointer-ness of references (one 
> level of indirection), then one set of use cases works and 
> people with alias-like use cases get surprised.

>    If the design embraces the alias-ness of references (no 
> indirection), then the other set of use cases works and people 
> with pointer-like use cases get surprised.

>    If the design mixes them, then a variety of people get 
> surprised in creative ways.


More information about the Digitalmars-d mailing list