www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template Question

reply Jiyan <jiyan jiyan.info> writes:
Hello,

i wanted to ask why this isnt working:

struct Text(T : char)
{
	size_t _len;
	T* _ptr;
}

Thanks :)
Nov 19 2017
next sibling parent reply Jiyan <jiyan jiyan.info> writes:
With working i mean that
Text X;
Doesnt compile!
Nov 19 2017
parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
parent Jiyan <jiyan jiyan.info> writes:
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:
 [...]
Okay. For starters, [...]
Ah ok thanks very much, this helped me a lot :)
Nov 19 2017
prev sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
parent reply Jiyan <jiyan jiyan.info> writes:
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:
 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
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 :)
Nov 19 2017
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 11/19/17 2:41 PM, Adam D. Ruppe wrote:
 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
If that's the case, then he needs to use Text(T = char) (default type) vs. Text(T : char) (specialization) -Steve
Nov 20 2017