`const ref T` or `ref const T`?

Paul Backus snarwin at gmail.com
Thu Aug 15 01:17:36 UTC 2024


On Thursday, 15 August 2024 at 00:35:33 UTC, Manu wrote:
> For me, it's a conceptual flow thing:
> const auto ref T goes: type stuff (const) -> storage class 
> stuff (auto ref)
> -> type stuff (T)
> auto ref const T goes: storage class stuff (auto ref) -> type 
> stuff (const T
> )
>
> It feels crude to interleave type stuff and storage class stuff 
> in an arbitrary way.
>
> The distinction feels more compelling when you consider that 
> parens may appear after type constructors (const(T)), and so in 
> those cases, the code MUST be written with the const 
> immediately preceding the type, forcing the storage class to 
> the left.

I guess the most consistent thing would be to always use the 
parentheses, and write `ref const(T)`.

> Similarly, allowing attributes on functions should likewise 
> appear after the function prototype:
>
> struct S {
>   const(T) method();
>   const T method(); // not the same; synonym for `T method() 
> const`
> }

My rule is that attributes that apply to `this` go on the right, 
and attributes that apply to the entire function go on the left. 
So:

     @safe nothrow T method() const;

     // Or on multiple lines:
     @safe nothrow
     T method() const;


More information about the Digitalmars-d mailing list