How to avoid code duplication in static if branches?
"Jérôme M. Berger"
jeberger at free.fr
Sat Mar 3 22:39:34 PST 2012
Andrej Mitrovic wrote:
> ...snip...
> I want to avoid writing "check()" twice. I only have to statically
> check a field of a member if it's of a certain type (Foo).
>
> One solution would be to use a boolean:
> void test(T)(T t)
> {
> bool isTrue = true;
> static if (is(T == Foo))
> isTrue = t.isTrue;
>
> if (isTrue)
> check();
> }
>
> But that kind of defeats the purpose of static if (avoiding runtime
> overhead). Does anyone have a trick up their sleeve for these types of
> situations? :)
There's always this:
void test(T)(T t)
{
static if (is (T == Foo))
if (!t.isTrue)
return;
check();
}
Jerome
--
mailto:jeberger at free.fr
http://jeberger.free.fr
Jabber: jeberger at jabber.fr
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: OpenPGP digital signature
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20120304/f83a1f00/attachment.pgp>
More information about the Digitalmars-d-learn
mailing list