Conditional Templates
Thomas Kuehne
thomas-dloop at kuehne.cn
Mon Jan 29 01:18:54 PST 2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Max Samukha schrieb am 2007-01-29:
> On Mon, 29 Jan 2007 17:28:51 +1100, Derek Parnell
><derek at nomail.afraid.org> wrote:
>
>>Help! The D documentation makes me seem such a fool.
>>
>>Here is what I want to do ...
>>
>>I want to create a template for a single class member method that behaves
>>differently depending on the type of the one parameter supplied to the
>>method. In other words, if the method is called with a integer type of
>>parameter, D should instantiate one form of the member method, and if the
>>parameter is a floating point data type, D should instantiate another form
>>of the member method, and finally if the parameter is a specific class, D
>>should instantiate yet another form of the member method.
>>
>>So in 'pseudo' code ...
>>
>>class Bar
>>{
>> template Foo(T)
>> {
>> static if (T is an integer type) // byte, ubyte, short, ushort, ...
>> {
>> void Foo(T x)
>> {
>> // do something with the integer 'x'
>> }
>> }
>>
>> static if (T is a floating point type) // float, double, real
>> {
>> void Foo(T x)
>> {
>> // do something with the floating point value.
>> }
>> }
>>
>> static if (T is the class 'Bar')
>> {
>> void Foo(Bar x)
>> {
>> // do something with this object.
>> }
>> }
>>
>> // All other forms are illegal.
>> }
>>}
>>
>>I've searched the docs and failed to solve how this can be done, if at all.
>>I'm sure there are some examples in the newsgroup postings but its like
>>looking for something that you don't know what it actually looks like.
>>
>>I did note that the docs say you can't use templates to add non-static
>>class members, but I tried and it works fine; so I don't know if the docs
>>are wrong, I'm wrong or DMD is wrong.
>
> With a mixin (if you want your method in more than one class):
>
> template TFoo(T)
> {
> static if (is(T == float) || is(T == real))
> {
> void Foo(T x)
> {
> writefln("In float foo: ", x);
> }
> }
> else static if (is(T == int) || is(T == short))
> {
> void Foo(T x)
> {
> writefln("In integer foo: ", x);
> }
> }
> else static assert("Bad type");
>
> }
I'd rather sugest
static if(is(T : long) || is(T : ulong))
// ...
else static if(is(T : real))
// ..
else
Thomas
-----BEGIN PGP SIGNATURE-----
iD8DBQFFvciULK5blCcjpWoRAn5nAKCpvyONxqqBpPdamnb/+NzTc+3aTQCePURd
jKCGiYXxaD3LH7nbUEJ0ysw=
=OnR+
-----END PGP SIGNATURE-----
More information about the Digitalmars-d-learn
mailing list