Conditional Templates

Derek Parnell derek at nomail.afraid.org
Mon Jan 29 17:17:49 PST 2007


On Mon, 29 Jan 2007 18:48:44 -0500, Jarrett Billingsley wrote:

> "Frits van Bommel" <fvbommel at REMwOVExCAPSs.nl> wrote in message 
> news:epkvl5$2vc1$1 at digitaldaemon.com...
>>
>> All integer types in D can implicitly convert to floating-point types. And 
>> he wants integer and float types to behave differently. In order to solve 
>> this use overloading you'd have to introduce a separate overload for every 
>> integer type to avoid ambiguous overloads. That's a lot of overloads :p...
> 
> Ahh.

Exactly. I started out by using function overloading but all the cut&paste
code got a bit obvious. 

Thanks for all who stopped by to help me. I now have the class working
(sort of).

** Things I have Learned **
(a) Walter, true to his admission, is not a good documenter. The current D
documentation provided by DigitalMars is a productivity sinkhole. Things
are hard, or nigh impossible to find, and if you do find  the thing you are
looking for, the explanation is usually too brief, and may or may not be
accurate.
  (I'll be soon adding to the Wiki-Doc-comments on a few items ).

(b) Templated member functions do not seem to be able to be used as
properties. In the example code given by Frits van Bommel, if you replace
the execution code ...

  b.Foo1(t);
  b.Foo2(t);

with 

  b.Foo1 = t;
  b.Foo2 = t;

you get errors along the lines of ...

 Error: (b).Foo2(T) has no value
 Error: (b).Foo2 is not an lvalue


(c) Current DMD is unable to help templates distinguish (accurately)
between character and integer types. 

I added some code to detect the 'char' family of types, but this just
caused all char and int types to fail.

   void Foo2(T)(T x) {
       static if (is(T : char)) {
               // do something with the char 'x'
               static assert(0, "No Char allowed");
               writefln("Foo2!(%s : char)(%x)", typeid(T), x);
       } else static if (is(T : int)) { // byte, ubyte, short, ushort, ...
               // do something with the integer 'x'
               writefln("Foo2!(%s : int)(%x)", typeid(T), x);
       } else static if (is(T : float)) { // float, double, real
               // do something with the floating point value.
               writefln("Foo2!(%s : float)(%f)", typeid(T), x);
       } else static if (is(T == Bar)) {
               // do something with this object.
               writefln("Foo2!(%s == Bar)(%s)", typeid(T), x);
       } else {         // All other forms are illegal.
           static assert(0,
               "Illegal instantiation of Bar with " ~ T.mangleof);
       }
     }

Then tested it using 'char' and it failed as expected. I then removed the
'char' types and it still failed ("No Char allowed"). I next removed all
the int types and then it worked. So it seems that D regards characters and
integers as interchangeable entities, which is just not correct. I know
that characters are implemented in memory as integers, but they are
conceptually different things, and just sometimes coders need to treat them
as different things. 


As a double-blind test, I went back to Frits' original code and replaced
  
	static if (is(T : int)) {

with

	static if (is(T : char)) {

and the test ran without failing. This indicates that D can't see any
difference between integer and character literals.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
30/01/2007 10:53:56 AM


More information about the Digitalmars-d-learn mailing list