digitalmars.D - Template questions
- Edward Diener (19/19) Mar 22 2008 I am going to be asking a number of questions about D templates in
- Jason House (14/38) Mar 22 2008 You'll help people out if you give us the URL of the page or file you're
- Edward Diener (5/36) Mar 22 2008 The first line in my example is 1.
- Bill Baxter (13/34) Mar 22 2008 struct TFoo(int x) { .. }
- Edward Diener (12/52) Mar 22 2008 Please explain the rules for this "shorthand". I can not find them in
- Frank Benoit (5/7) Mar 22 2008 http://www.digitalmars.com/d/1.0/template.html
- Edward Diener (6/14) Mar 22 2008 Thanks !I saw the example incorrectly. The example I gave had:
- BCS (4/7) Mar 22 2008 I don't know how old the .pdf is but the .html is general considered to ...
I am going to be asking a number of questions about D templates in various posts, and I hope others can help me understand D templates despite the documentation. First question: In the documentation on templates I read these lines: "Even if template arguments are implicitly converted to the same template parameter type, they still refer to different instances: struct TFoo(int x) { } static assert(is(TFoo!(3) == TFoo!(2 + 1))); // 3 and 2+1 are both 3 of // type int static assert(!is(TFoo!(3) == TFoo!(3u))); // 3u and 3 are different // types" To what is the "int" on line 6 refer ? Why is line 3 considered a D template ? It does not begin with 'template ...'. It looks like a simple 'struct' to me with a public member. How then can it be instantiated as if it were a template ? Otherwise I do understand what is being said about templates being instantiated to different instances.
Mar 22 2008
Edward Diener wrote:I am going to be asking a number of questions about D templates in various posts, and I hope others can help me understand D templates despite the documentation. First question: In the documentation on templates I read these lines: "Even if template arguments are implicitly converted to the same template parameter type, they still refer to different instances: struct TFoo(int x) { } static assert(is(TFoo!(3) == TFoo!(2 + 1))); // 3 and 2+1 are both 3 of // type int static assert(!is(TFoo!(3) == TFoo!(3u))); // 3u and 3 are different // types" To what is the "int" on line 6 refer ? Why is line 3 considered a D template ? It does not begin with 'template ...'. It looks like a simple 'struct' to me with a public member. How then can it be instantiated as if it were a template ? Otherwise I do understand what is being said about templates being instantiated to different instances.You'll help people out if you give us the URL of the page or file you're looking at, for example http://www.digitalmars.com/d/2.0/template.html How are you extracting line numbers? Here's text I found online (reformatted to avoid word wrap issues) struct TFoo(int x) { } // 3 and 2+1 are both 3 of type int static assert(is(TFoo!(3) == TFoo!(2 + 1))); // 3u and 3 are different types static assert(!is(TFoo!(3) == TFoo!(3u))); Sadly, I can't help because this doesn't make sense to me either. The template parameter is of type int, so I'd expect 3 and 3u to be converted to an int (3) to fit the specified type... and therefore be of the same type.
Mar 22 2008
Jason House wrote:Edward Diener wrote:It is straight from the pdf documentation.I am going to be asking a number of questions about D templates in various posts, and I hope others can help me understand D templates despite the documentation. First question: In the documentation on templates I read these lines: "Even if template arguments are implicitly converted to the same template parameter type, they still refer to different instances: struct TFoo(int x) { } static assert(is(TFoo!(3) == TFoo!(2 + 1))); // 3 and 2+1 are both 3 of // type int static assert(!is(TFoo!(3) == TFoo!(3u))); // 3u and 3 are different // types" To what is the "int" on line 6 refer ? Why is line 3 considered a D template ? It does not begin with 'template ...'. It looks like a simple 'struct' to me with a public member. How then can it be instantiated as if it were a template ? Otherwise I do understand what is being said about templates being instantiated to different instances.You'll help people out if you give us the URL of the page or file you're looking at, for example http://www.digitalmars.com/d/2.0/template.htmlHow are you extracting line numbers?The first line in my example is 1. I will assume the 'int' is a typo which no one has seen fit to correct or update in the PDF documentation.
Mar 22 2008
Edward Diener wrote:I am going to be asking a number of questions about D templates in various posts, and I hope others can help me understand D templates despite the documentation. First question: In the documentation on templates I read these lines: "Even if template arguments are implicitly converted to the same template parameter type, they still refer to different instances: struct TFoo(int x) { } static assert(is(TFoo!(3) == TFoo!(2 + 1))); // 3 and 2+1 are both 3 of // type int static assert(!is(TFoo!(3) == TFoo!(3u))); // 3u and 3 are different // types" To what is the "int" on line 6 refer ? Why is line 3 considered a D template ? It does not begin with 'template ...'. It looks like a simple 'struct' to me with a public member. How then can it be instantiated as if it were a template ?struct TFoo(int x) { .. } is shorthand in D for template TFoo(int x) { struct TFoo { .. } } Just like how with D function templates this void Foo(int x)() { .. } is shorthand for this: template Foo(int x) { void Foo() { .. } } --bb
Mar 22 2008
Bill Baxter wrote:Edward Diener wrote:Please explain the rules for this "shorthand". I can not find them in the PDF documenatation. Is the rule that this applies only if there is a single data field in the struct/class class, with no other data or member functions of any kind, or a single parameter in the function case ? If that is the rule, how does one define a class/struct with a single daya parameter which is not a template, or does one just give up on such a type ? If that is the rule how does one define a function which takes a single parameter which is not a template function, or does one just give up on such a function ?I am going to be asking a number of questions about D templates in various posts, and I hope others can help me understand D templates despite the documentation. First question: In the documentation on templates I read these lines: "Even if template arguments are implicitly converted to the same template parameter type, they still refer to different instances: struct TFoo(int x) { } static assert(is(TFoo!(3) == TFoo!(2 + 1))); // 3 and 2+1 are both 3 of // type int static assert(!is(TFoo!(3) == TFoo!(3u))); // 3u and 3 are different // types" To what is the "int" on line 6 refer ? Why is line 3 considered a D template ? It does not begin with 'template ...'. It looks like a simple 'struct' to me with a public member. How then can it be instantiated as if it were a template ?struct TFoo(int x) { .. } is shorthand in D for template TFoo(int x) { struct TFoo { .. } } Just like how with D function templates this void Foo(int x)() { .. } is shorthand for this: template Foo(int x) { void Foo() { .. } }
Mar 22 2008
Edward Diener schrieb:Please explain the rules for this "shorthand". I can not find them in the PDF documenatation.http://www.digitalmars.com/d/1.0/template.html See Class Template and ff. "If a template declares exactly one member, and that member is a class with the same name as the template:"
Mar 22 2008
Frank Benoit wrote:Edward Diener schrieb:Thanks !I saw the example incorrectly. The example I gave had: struct TFoo(int x) { } and somehow I saw this as: struct TFoo { int x; } That is definitely my error.Please explain the rules for this "shorthand". I can not find them in the PDF documenatation.http://www.digitalmars.com/d/1.0/template.html See Class Template and ff. "If a template declares exactly one member, and that member is a class with the same name as the template:"
Mar 22 2008
Reply to Edward,I can not find them in the PDF documenatation.I don't know how old the .pdf is but the .html is general considered to be more authoritative. I don't know if it makes a difference in this case except that more people have them handy than the .pdf.
Mar 22 2008