Default template arguments

Tobias Pankrath tobias at pankrath.net
Mon Nov 19 10:02:34 PST 2012


On Monday, 19 November 2012 at 17:50:59 UTC, Ali Çehreli wrote:
> On 11/19/2012 09:44 AM, bearophile wrote:
>> Better:
>>
>> import std.stdio;
>>
>> private void printSizeHelper(T)(T x) {
>> writeln(T.stringof);
>> }
>>
>> void printSize(T1 = void, T2)(T2 x) {
>> static if (is(T1 == void))
>> printSizeHelper!real(x);
>> else
>> printSizeHelper!T1(x);
>> }
>>
>> void main() {
>> float x;
>> x.printSize();
>> x.printSize!float();
>> x.printSize!real();
>> }
>>
>>
>> (Keeping the two functions separated is useful against 
>> template bloat.)
>>
>> Bye,
>> bearophile
>
> I like that! :)
>
> Is it possible or required to ensure that T1 is T2 in the 
> static else clause?
>
> Ali

Should check if it's implicit convertable. Most of the times you 
use this pattern, you'll get an template error down in 
printSizeHelper, but you want your template hierarchy to fail as 
soon as possible.




More information about the Digitalmars-d-learn mailing list