digitalmars.D.learn - Question about tuple as template parameter
- Max Samukha (13/13) Apr 22 2008 Why the compiler chooses the template with tuple paramer? Isn't the
- Bill Baxter (15/31) Apr 22 2008 I don't think the spec makes any promises about that sort of thing.
- Max Samukha (6/37) Apr 22 2008 Yeah, I did something similar.
Why the compiler chooses the template with tuple paramer? Isn't the template with type parameter more 'specialized'? template Foo(T) { pragma(msg, "Type"); } template Foo(TT...) { pragma(msg, "Tuple"); } alias Foo!(int) foo; ---- Outputs: Tuple
Apr 22 2008
Max Samukha wrote:Why the compiler chooses the template with tuple paramer? Isn't the template with type parameter more 'specialized'? template Foo(T) { pragma(msg, "Type"); } template Foo(TT...) { pragma(msg, "Tuple"); } alias Foo!(int) foo; ---- Outputs: TupleI don't think the spec makes any promises about that sort of thing. Generally speaking you're on shaky ground any time you try to overload templates in D. Can you not put a "static if(TT.length==1)" in the tuple version? Also you could try making the tuple one be template Foo(T0,T1,TN...) { alias Tuple!(T0,T1,TN) TT; } So that it takes a min of 2 args to differentiate. And then add a zero arg version if you need that too. But I think Walter's idea with templates is that to make it simpler any ambiguity should just be an error. So that you don't end up with a bunch of crazy rules that no two compilers implement quite the same. --bb
Apr 22 2008
On Tue, 22 Apr 2008 20:05:21 +0900, Bill Baxter <dnewsgroup billbaxter.com> wrote:Max Samukha wrote:Yeah, I did something similar.Why the compiler chooses the template with tuple paramer? Isn't the template with type parameter more 'specialized'? template Foo(T) { pragma(msg, "Type"); } template Foo(TT...) { pragma(msg, "Tuple"); } alias Foo!(int) foo; ---- Outputs: TupleI don't think the spec makes any promises about that sort of thing. Generally speaking you're on shaky ground any time you try to overload templates in D. Can you not put a "static if(TT.length==1)" in the tuple version? Also you could try making the tuple one be template Foo(T0,T1,TN...) { alias Tuple!(T0,T1,TN) TT; } So that it takes a min of 2 args to differentiate. And then add a zero arg version if you need that too.But I think Walter's idea with templates is that to make it simpler any ambiguity should just be an error. So that you don't end up with a bunch of crazy rules that no two compilers implement quite the same. --bbAgree that this should probably result in an error. There is also an inconsistency in the current implementation (dmd 2.012, http://d.puremagic.com/issues/show_bug.cgi?id=2025).
Apr 22 2008