digitalmars.D.learn - Template Question
- Jiyan (8/8) Nov 19 2017 Hello,
- Jiyan (3/3) Nov 19 2017 With working i mean that
- Jonathan M Davis (48/51) Nov 19 2017 Okay. For starters,
- Jiyan (3/8) Nov 19 2017 Ah ok thanks very much, this helped me a lot :)
- Jonathan M Davis (4/12) Nov 19 2017 What about it isn't working? I think that you need to explain what you'r...
- Jiyan (7/24) Nov 19 2017 Text X;
- Adam D. Ruppe (4/5) Nov 19 2017 You still need to instantiate it, even with default args.
- Steven Schveighoffer (4/13) Nov 20 2017 If that's the case, then he needs to use Text(T = char) (default type)
Hello, i wanted to ask why this isnt working: struct Text(T : char) { size_t _len; T* _ptr; } Thanks :)
Nov 19 2017
On Sunday, November 19, 2017 19:25:40 Jiyan via Digitalmars-d-learn wrote:With working i mean that Text X; Doesnt compile!Okay. For starters, struct Text(T : char) { size_t _len; T* _ptr; } is a template specialization, which means that it's only going to compile if T is char or a type that implicitly converts to char. It's similar (but not quite the same as) doing struct Text(T) if(is(T : char)) { } https://dlang.org/spec/template.html#TemplateTypeParameterSpecialization I suspect that what you meant to do was struct Text(T = char) { size_t _len; T* _ptr; } which would make char the default value for T. However, not even that is going to allow you to do Text t; There is no implicit instantiation of templated types. Having the default value for T will allow you to do Text!() x; instead of Text!char x; but it won't allow you to do Text x; and there's no way to do that. Templated types must always be explicitly instantiated. What you could do that would give you a similar effect would be to do struct TextImpl(T) { size_t len; T* ptr; } alias Text = TextImpl!char; and then when you use Text, it will be replaced with TextImpl!char, which would allow you to do Text x; The only time that templates are ever implicitly instantiated is with function templates where all of the template parameters can be inferred from the function arguments. It never occurs with any other kind of template, even if the template has no parameters. - Jonathan M Davis
Nov 19 2017
On Sunday, 19 November 2017 at 19:42:02 UTC, Jonathan M Davis wrote:On Sunday, November 19, 2017 19:25:40 Jiyan via Digitalmars-d-learn wrote:Ah ok thanks very much, this helped me a lot :)[...]Okay. For starters, [...]
Nov 19 2017
On Sunday, November 19, 2017 19:22:51 Jiyan via Digitalmars-d-learn wrote:Hello, i wanted to ask why this isnt working: struct Text(T : char) { size_t _len; T* _ptr; } Thanks :)What about it isn't working? I think that you need to explain what you're trying to do and what you think that this code should be doing but isn't. - Jonathan M Davis
Nov 19 2017
On Sunday, 19 November 2017 at 19:28:37 UTC, Jonathan M Davis wrote:On Sunday, November 19, 2017 19:22:51 Jiyan via Digitalmars-d-learn wrote:Text X; Here X would be an Object from type Text with T being char when i'm right. But this doesn't compile! Thanks :)Hello, i wanted to ask why this isnt working: struct Text(T : char) { size_t _len; T* _ptr; } Thanks :)What about it isn't working? I think that you need to explain what you're trying to do and what you think that this code should be doing but isn't. - Jonathan M Davis
Nov 19 2017
On Sunday, 19 November 2017 at 19:31:53 UTC, Jiyan wrote:Text X;You still need to instantiate it, even with default args. Text!() X; will work
Nov 19 2017
On 11/19/17 2:41 PM, Adam D. Ruppe wrote:On Sunday, 19 November 2017 at 19:31:53 UTC, Jiyan wrote:If that's the case, then he needs to use Text(T = char) (default type) vs. Text(T : char) (specialization) -SteveText X;You still need to instantiate it, even with default args. Text!() X; will work
Nov 20 2017