Kitz ADSL Broadband Information
adsl spacer  
Support this site
Home Broadband ISPs Tech Routers Wiki Forum
   Compare ISP   Rate your ISP
   Glossary   Glossary
Please login or register.

Login with username, password and session length
Advanced search  


Author Topic: D lang - fn template specialisation if argument's value is known at compile-time  (Read 4306 times)


  • Senior Kitizen
  • ******
  • Posts: 11459
  • Retd s/w dev; A&A; 4x7km ADSL2 lines; Firebrick

A snippet of D lang source code that does not work, a templated generic function. (T is some type which could be anything that makes sense; it is intended to be a uint of various possible widths and so generate different functions one lump of code for each type) :

T myfunc(T)( T x, T mask )
   if ( mask == 3 )
   return fast_func( x, mask );

but of course this doesn't work, the compiler hates it, because mask is not known at compile-time. so I wondered if there is a way to do something like static if ( isKnownAtCompileTime( mask ) ) but that would not necessarily help me and probably isn't the right way.

Basically there is a fast path for certain known values of a (second in this case) argument where the compiler could produce superb trivial code or where I can work out a shortcut myself. for example myfunc( x, 0 ) == 0 and myfunc( x, -1 ) == x and various other good things, and for some values of mask the thing behaves like an AND operation so I want the compiler to just generate that.

The default slow path where the arg is unknown involves calling asm so the compiler cannot use its intelligence as it does not know the detailed semantics.


To add further complication: if both arguments of myfunc() are known at compile-time, then I definitely want to take an alternative path because then I can apply D CTFE and calculate a compile-time result.

[For those familiar with C++, the first pair of ( ) is like the < > in C++ template declarations but without the hilarious syntax ambiguity cockup. The second pair of brackets are a normal function argument list. You can put a load of different kinds of things inside the first pair of round brackets and these are known at compile time and are for customising/parametrising the whole template. Types are typically listed in there. The if afterwards is optional, it is one way of generating various alternative templates by giving a compile-time condition that must match if the template is to be chosen from a number of alternative candidates.

To invoke a template or call a templated function you may use the following kind of syntax this_func!(template_arg1, template_arg2)( p1, p2, p3 ) but the !() first part can be left out if it is not needed by the compiler, for example when it contains types and the compiler just sees what those types are in a particular generic templated function call.

The first snippet shows an abbreviated syntax for declaring templated stuff. There is also a longer syntax that uses the keyword 'template' plus a compile-time argument list for template customisation and then a block, and that can generate a number of different declarations from the one block, and is more powerful. I am afraid that more info on this involves rtfm.]