Type-qualified functions?
Steven Schveighoffer
schveiguy at yahoo.com
Fri Jan 21 07:17:17 PST 2011
On Fri, 21 Jan 2011 09:08:58 -0500, Sean Eskapp <eatingstaples at 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
More information about the Digitalmars-d-learn
mailing list