Optimal implementation of identity function

Per Nordlöw per.nordlow at gmail.com
Thu Dec 20 13:39:02 UTC 2018


For the default case (`return key`) in

     auto adjustKeyType(SomeKey)(const return scope SomeKey key) 
const @trusted
     {
         pragma(inline, true);            // must be inlined
         static if (is(SomeKey : U[], U)) // is array
         {
             /* because return value is used only temporarily it's 
ok to cast to
              * `immutable` to prevent GC-allocations in types 
such as
              * `sso_string.SSOString` */
             return cast(immutable(typeof(key[0]))[])key;
         }
         else
         {
             return key;
         }
     }

I want this function to be optimized away completely when inlined 
in release mode.

Is this the best of achieving this with regards to

1. its return type: auto (or is better auto ref) and
2. parameter `key` being qualified as `return scope`

?


More information about the Digitalmars-d-learn mailing list