www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Question about tuple as template parameter

reply Max Samukha <nospam nospam.com> writes:
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
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
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: Tuple

I 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
parent Max Samukha <nospam nospam.com> writes:
On Tue, 22 Apr 2008 20:05:21 +0900, Bill Baxter
<dnewsgroup billbaxter.com> wrote:

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: Tuple

I 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.

Yeah, I did something similar.
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

Agree 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