On Fri, 21 Jan 2011 09:08:58 -0500, Sean Eskapp <eatingstap...@gmail.com> wrote:

How does one avoid code duplication in a snippet code like this:

class A{}

void foo(const A, void delegate(const A) fn)
{
        // some stuff
        // ...
        // ...
}

void foo(A, void delegate(A) fn)
{
        // exact same stuff, with different qualifiers
        // ...
        // ...
}

templates:

void foo(T)(T, void delegate(T) fn)
{
}

This parameterizes foo based on T, which could be A, const A, or int, or whatever works to compile the function.

You could further restrict which templates can be instantiated with a template constraint (say for instance, to restring foo to only instantiate when T is A or const(A) ).

-Steve

Reply via email to