Re: function template specialization question D vs. C++

2018-01-14 Thread kdevel via Digitalmars-d-learn
On Sunday, 14 January 2018 at 02:24:52 UTC, Adam D. Ruppe wrote: On Sunday, 14 January 2018 at 02:14:50 UTC, Jonathan M Davis wrote: If you're using template constraints rather than template specializations, then you can't have any unconstrained templates. Not true: see the tip of the week he

Re: function template specialization question D vs. C++

2018-01-13 Thread Jonathan M Davis via Digitalmars-d-learn
On Saturday, January 13, 2018 19:32:06 Jonathan M Davis via Digitalmars-d- learn wrote: > On Sunday, January 14, 2018 02:24:52 Adam D. Ruppe via Digitalmars-d-learn > wrote: > > On Sunday, 14 January 2018 at 02:14:50 UTC, Jonathan M Davis > > > > wrote: > > > If you're using template constraints ra

Re: function template specialization question D vs. C++

2018-01-13 Thread Adam D. Ruppe via Digitalmars-d-learn
On Sunday, 14 January 2018 at 00:09:42 UTC, kdevel wrote: The compiler does not allow me to specialize the primary function template for double: Yes, it does., and it works for float and double. Just `real` matches both equally, so that's the error for that type. Let me quote the spec: http

Re: function template specialization question D vs. C++

2018-01-13 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, January 14, 2018 02:24:52 Adam D. Ruppe via Digitalmars-d-learn wrote: > On Sunday, 14 January 2018 at 02:14:50 UTC, Jonathan M Davis > > wrote: > > If you're using template constraints rather than template > > specializations, then you can't have any unconstrained > > templates. > > No

Re: function template specialization question D vs. C++

2018-01-13 Thread Adam D. Ruppe via Digitalmars-d-learn
On Sunday, 14 January 2018 at 02:14:50 UTC, Jonathan M Davis wrote: If you're using template constraints rather than template specializations, then you can't have any unconstrained templates. Not true: see the tip of the week here http://arsdnet.net/this-week-in-d/2016-sep-04.html

Re: function template specialization question D vs. C++

2018-01-13 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, January 14, 2018 01:02:46 kdevel via Digitalmars-d-learn wrote: > On Sunday, 14 January 2018 at 00:30:37 UTC, Nicholas Wilson wrote: > > The usual way to do what you are trying to do is with template > > constraints. > > > > void foo(T)() if (is(T== float)) { ...} > > Thanks. That works

Re: function template specialization question D vs. C++

2018-01-13 Thread kdevel via Digitalmars-d-learn
On Sunday, 14 January 2018 at 00:30:37 UTC, Nicholas Wilson wrote: The usual way to do what you are trying to do is with template constraints. void foo(T)() if (is(T== float)) { ...} Thanks. That works but looks a bit ugly. Am I right that I have to leave out the primary (unconstrained) temp

Re: function template specialization question D vs. C++

2018-01-13 Thread Nicholas Wilson via Digitalmars-d-learn
On Sunday, 14 January 2018 at 00:09:42 UTC, kdevel wrote: fusp.d ``` import std.stdio; import std.typecons; void foo (T) () { writeln ("(1) foo T = ", T.stringof); } void foo (T : float) () { writeln ("(2) foo T = ", T.stringof); } // void foo (T : double) () // { //writeln ("(2) foo

function template specialization question D vs. C++

2018-01-13 Thread kdevel via Digitalmars-d-learn
fusp.d ``` import std.stdio; import std.typecons; void foo (T) () { writeln ("(1) foo T = ", T.stringof); } void foo (T : float) () { writeln ("(2) foo T = ", T.stringof); } // void foo (T : double) () // { //writeln ("(2) foo T = ", T.stringof); // } void main () { foo!float;

Re: Template specialization question

2016-11-24 Thread Steven Schveighoffer via Digitalmars-d-learn
On Thursday, 24 November 2016 at 17:59:55 UTC, ag0aep6g wrote: Took me a bit to find it, but it is documented: "If both a template with a sequence parameter and a template without a sequence parameter exactly match a template instantiation, the template without a TemplateSequenceParameter is

Re: Template specialization question

2016-11-24 Thread Stefan Koch via Digitalmars-d-learn
On Thursday, 24 November 2016 at 17:47:04 UTC, Steven Schveighoffer wrote: void foo(T)(T t){writeln("a");} void foo(T...)(T t){writeln("b");} foo(1); Compiles? If so, which prints out? I was surprised by the answer. I can't find docs for it. Is the behavior intended? -Steve That is expect

Re: Template specialization question

2016-11-24 Thread ag0aep6g via Digitalmars-d-learn
On 11/24/2016 06:47 PM, Steven Schveighoffer wrote: void foo(T)(T t){writeln("a");} void foo(T...)(T t){writeln("b");} foo(1); Compiles? If so, which prints out? I was surprised by the answer. I can't find docs for it. Is the behavior intended? Took me a bit to find it, but it is documented:

Template specialization question

2016-11-24 Thread Steven Schveighoffer via Digitalmars-d-learn
void foo(T)(T t){writeln("a");} void foo(T...)(T t){writeln("b");} foo(1); Compiles? If so, which prints out? I was surprised by the answer. I can't find docs for it. Is the behavior intended? -Steve

Re: template specialization question

2010-02-01 Thread Ellery Newcomer
On 02/01/2010 07:29 PM, Ali Çehreli wrote: daoryn wrote: > According to http://digitalmars.com/d/2.0/template.html it is possible to specify template specialization so that DMD prefers them when instanciating templates, however the following code: > > > - > im

Re: template specialization question

2010-02-01 Thread Ali Çehreli
daoryn wrote: > According to http://digitalmars.com/d/2.0/template.html it is possible to specify template specialization so that DMD prefers them when instanciating templates, however the following code: > > > - > import std.stdio; > > void print(T)(T thing) > {

Re: template specialization question

2010-02-01 Thread Ellery Newcomer
On 02/01/2010 04:19 PM, daoryn wrote: The whole point of specialisation (and of templates in general) is to have functions that work for any type. Having to forcibly specify a type is like casting to a specific overload of a function. Why add clutter to the syntax when the language advertises

Re: template specialization question

2010-02-01 Thread daoryn
Tomek Sowiński Wrote: > Dnia 31-01-2010 o 21:39:21 Ali Çehreli napisał(a): > > > � wrote: > >> Dnia 31-01-2010 o 20:59:47 Tomek Sowi�ski napisa�(a): > >> > >>> // specialization needed to limit matching types > >>> void print(T:int)(T thing) > >> To be clear -- I did this to silence t

Re: template specialization question

2010-02-01 Thread daoryn
> It works with dmd 2.040 without the :int specialization. No it doesnt. Did you use specific compiler flags? Also check that you are using the source I posted and not the modified versions presented which served only to circumvent compiler warnings and not answer the original question: is it a

Re: template specialization question

2010-02-01 Thread daoryn
Ellery Newcomer Wrote: > I haven't gotten around to templates yet, so I don't grok them quite as > well as I'd like, but it looks like DMD is having trouble deducing T > from the parameter given. > > print([1,2,3]) fails to match the specialized template, even when the > general template is re

Re: template specialization question

2010-02-01 Thread daoryn
Daniel Murphy Wrote: > daoryn Wrote: > > > According to http://digitalmars.com/d/2.0/template.html it is possible to > > specify template specialization so that DMD prefers them when instanciating > > templates, however the following code: > > > > > > - > > imp

Re: template specialization question

2010-01-31 Thread Tomek Sowiński
Dnia 31-01-2010 o 21:39:21 Ali Çehreli napisał(a): � wrote: Dnia 31-01-2010 o 20:59:47 Tomek Sowi�ski napisa�(a): // specialization needed to limit matching types void print(T:int)(T thing) To be clear -- I did this to silence the compiler saying the call with array matches more than one

Re: template specialization question

2010-01-31 Thread Ali Çehreli
� wrote: Dnia 31-01-2010 o 20:59:47 Tomek Sowi�ski napisa�(a): // specialization needed to limit matching types void print(T:int)(T thing) To be clear -- I did this to silence the compiler saying the call with array matches more than one function template declaration. I'm not sure whether

Re: template specialization question

2010-01-31 Thread Tomek Sowiński
Dnia 31-01-2010 o 20:59:47 Tomek Sowiński napisał(a): // specialization needed to limit matching types void print(T:int)(T thing) To be clear -- I did this to silence the compiler saying the call with array matches more than one function template declaration. I'm not sure whether the comp

Re: template specialization question

2010-01-31 Thread Tomek Sowiński
Dnia 31-01-2010 o 19:49:44 daoryn napisał(a): import std.stdio; void print(T)(T thing) { writeln("Calling print(T)"); writeln(T.stringof); } void print(T:T[])(T[] things) { writeln("Calling print(T[])"); writeln(T.stringof); } void main() { print(3);

Re: template specialization question

2010-01-31 Thread Ellery Newcomer
I haven't gotten around to templates yet, so I don't grok them quite as well as I'd like, but it looks like DMD is having trouble deducing T from the parameter given. print([1,2,3]) fails to match the specialized template, even when the general template is removed. If you force the template

Re: template specialization question

2010-01-31 Thread Daniel Murphy
daoryn Wrote: > According to http://digitalmars.com/d/2.0/template.html it is possible to > specify template specialization so that DMD prefers them when instanciating > templates, however the following code: > > > - > import std.stdio; > > void print(T)(T thin

template specialization question

2010-01-31 Thread daoryn
According to http://digitalmars.com/d/2.0/template.html it is possible to specify template specialization so that DMD prefers them when instanciating templates, however the following code: - import std.stdio; void print(T)(T thing) { writeln("Calling pri