www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - template condition

reply "Namespace" <rswhite4 googlemail.com> writes:
Is there any difference between these two code snippets:


struct Foo(T : Object) {


struct Foo(T) if (is(T == class)) {

?



Sep 27 2012
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 09/27/2012 03:01 PM, Namespace wrote:
 Is there any difference between these two code snippets:


 struct Foo(T : Object) {


 struct Foo(T) if (is(T == class)) {

 ?



Yes there is: struct S{ Object o; alias o this; }
Sep 27 2012
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 9/27/12, Timon Gehr <timon.gehr gmx.ch> wrote:
 snip
Perhaps he meant something like this: struct Foo(T : Object) { } struct Foo(T) if (is(T : Object )) { } void main() { Foo!Object x; } The difference is the first template is a specialization, and the second has a constraint. The first one will match better than the second one even though both templates can be instantiated with 'Object'. The constraint version is more flexible but the specialization might be shorter to write. http://dlang.org/template.html
Sep 27 2012
parent reply "Namespace" <rswhite4 googlemail.com> writes:
Ok. And there is no performance difference? Just for the sake of 
completeness. :)
Sep 27 2012
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 27 Sep 2012 11:34:15 -0400, Namespace <rswhite4 googlemail.com>  
wrote:

 Ok. And there is no performance difference? Just for the sake of  
 completeness. :)
performance where? Template instantiation is done at compile time. The resulting code should be unaffected runtime-performance wise. -Steve
Sep 27 2012
parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Thursday, 27 September 2012 at 15:39:39 UTC, Steven 
Schveighoffer wrote:
 On Thu, 27 Sep 2012 11:34:15 -0400, Namespace 
 <rswhite4 googlemail.com> wrote:

 Ok. And there is no performance difference? Just for the sake 
 of completeness. :)
performance where? Template instantiation is done at compile time. The resulting code should be unaffected runtime-performance wise. -Steve
I mean: is there any difference by building the template? I don't understand what "more flexible" exactly mean.
Sep 27 2012
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 9/27/12, Namespace <rswhite4 googlemail.com> wrote:
 I mean: is there any difference by building the template? I don't
 understand what "more flexible" exactly mean.
I mean you can create more complex constraints. See http://dlang.org/template.html
Sep 27 2012
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 27 Sep 2012 11:46:05 -0400, Namespace <rswhite4 googlemail.com>  
wrote:

 On Thursday, 27 September 2012 at 15:39:39 UTC, Steven Schveighoffer  
 wrote:
 On Thu, 27 Sep 2012 11:34:15 -0400, Namespace <rswhite4 googlemail.com>  
 wrote:

 Ok. And there is no performance difference? Just for the sake of  
 completeness. :)
performance where? Template instantiation is done at compile time. The resulting code should be unaffected runtime-performance wise. -Steve
I mean: is there any difference by building the template? I don't understand what "more flexible" exactly mean.
The constraint basically is saying whether to instantiate the template or not, it has little to do with runtime performance. Flexible means you have a full boolean logic you can use. For example, if you had two unrelated objects A and B, how would you instantiate a template if the parameter derives from A *or* derives from B? With a template constraint, that's: template tmp(T) if(is(T : A) || is(T : B)) This is impossible to do with a specialization. However, there are some limitations. Template constraints don't cascade, while specializations do. So while this: template tmp(T) ... template tmp(T : A) works fine, this: template tmp(T) ... template tmp(T) if (is(T : A)) ... does not, because it can instantiate both with an A. Instead you have to do: template tmp(T) if(!is(T : A)) ... template tmp(T) if(is(T : A)) ... It has been proposed to try and use else (if), but Walter didn't like it. -Steve
Sep 27 2012