Persistent list

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Mon Nov 16 14:02:20 PST 2015


On Monday, 16 November 2015 at 21:47:07 UTC, Andrei Alexandrescu 
wrote:
> On 11/16/2015 04:18 PM, Jonathan M Davis wrote:
>> When you can templatize the function on the type that would 
>> otherwise be
>> inout, then there really isn't any reason to use inout. But 
>> what do you
>> do when you're dealing with a member function - especially on 
>> a class? I
>> don't know of any way to templatize a member function on the 
>> type of the
>> this pointer/reference, but maybe there's a template feature 
>> with which
>> I'm not familiar enough. But even if there _is_ such a 
>> feature, it won't
>> work for virtual functions.
>
> It does. Write one-liners that forward to the template 
> function, as shown in http://dpaste.dzfl.pl/52a3013efe34 
> (search the page for QList). -- Andrei

Well, that does reduce the code duplication, but you're still 
forced to have up to three separate functions to get each of the 
different return types (even if they are then simply wrapper 
functions). And compare that with simply declaring the same 
function once with its implementation internal to itself. It's 
just that you use inout instead of const. How is that worse?

There are cases where inout doesn't work as well as const does 
due to limitations with inout, but in many (most?) cases, the 
only difference between using const and inout is that you're 
typing const instead of inout. So, if anything, that makes it 
sound like fixing inout so that it works in those other cases 
would be better than avoiding it in favor of splitting out the 
implementation into a separate function and having multiple 
wrapper functions in the API.

- Jonathan M Davis


More information about the Digitalmars-d mailing list