View mode: basic / threaded / horizontal-split · Log in · Help
November 17, 2012
template error instantiating
I get "template instance main.MyClass!(int) error instantiating" 
in the following case:

public class MyClass(TStuff)
{
	public void foo(TStuff item)
	{}

	public void foo(TRange)(TRange items)
	if (isInputRange!TRange && is(ElementType!TRange == TStuff))
	{}
}

void main(string[] argv)
{
	MyClass!int instance = new MyClass!int();

	return;
}

but if i rename one of the "foo" overloads then code will be 
compiled.
November 17, 2012
Re: template error instantiating
On Saturday, 17 November 2012 at 18:28:10 UTC, ref2401 wrote:
> I get "template instance main.MyClass!(int) error 
> instantiating" in the following case:
>
> public class MyClass(TStuff)
> {
> 	public void foo(TStuff item)
> 	{}
>
> 	public void foo(TRange)(TRange items)
> 	if (isInputRange!TRange && is(ElementType!TRange == TStuff))
> 	{}
> }
>
> void main(string[] argv)
> {
> 	MyClass!int instance = new MyClass!int();
>
> 	return;
> }
>
> but if i rename one of the "foo" overloads then code will be 
> compiled.

Full error message is:

Error: template main.MyClass!(int).MyClass.foo(TRange) if 
(isInputRange!(TRange) && is(ElementType!(TRange) == TStuff)) 
conflicts with function main.MyClass!(int).MyClass.foo at 
main.d(3)
main.d(13): Error: template instance main.MyClass!(int) error 
instantiating

That's why renaming foo makes dmd happy. Alternatively you can 
add extra dummy parenthesis in the first foo to make it template 
function. This is not the first case I see adding parenthesis to 
function declarations solves compilation errors.
November 17, 2012
Re: template error instantiating
Maxim Fomin wrote:

> solves compilation errors

From the docs:
| If a template has exactly one member in it, and the name of
| that member is the same as the template name, that member
| is assumed to be referred to in a template instantiation

It seems that the problem is located in this definition.

-manfred
November 17, 2012
Re: template error instantiating
On Saturday, 17 November 2012 at 20:44:05 UTC, Manfred Nowak 
wrote:
> Maxim Fomin wrote:
>
>> solves compilation errors
>
> From the docs:
> | If a template has exactly one member in it, and the name of
> | that member is the same as the template name, that member
> | is assumed to be referred to in a template instantiation
>
> It seems that the problem is located in this definition.
>
> -manfred

How eponymous templates are related to the issue?
November 17, 2012
Re: template error instantiating
On Saturday, 17 November 2012 at 18:28:10 UTC, ref2401 wrote:
> I get "template instance main.MyClass!(int) error 
> instantiating" in the following case:
>
> public class MyClass(TStuff)
> {
> 	public void foo(TStuff item)
> 	{}
>
> 	public void foo(TRange)(TRange items)
> 	if (isInputRange!TRange && is(ElementType!TRange == TStuff))
> 	{}
> }
>
> void main(string[] argv)
> {
> 	MyClass!int instance = new MyClass!int();
>
> 	return;
> }
>
> but if i rename one of the "foo" overloads then code will be 
> compiled.

Template functions are currently not overloadable with 
non-template ones. As a workaround (if foo(TStuff) doesn't need 
to be virtual), you can turn it into a template function (without 
parameters, i.e. foo()(TStuff item)) as well.

David
November 17, 2012
Re: template error instantiating
On 11/17/2012 07:28 PM, ref2401 wrote:
> I get "template instance main.MyClass!(int) error instantiating" in the
> following case:
>
> public class MyClass(TStuff)
> {
>      public void foo(TStuff item)
>      {}
>
>      public void foo(TRange)(TRange items)
>      if (isInputRange!TRange && is(ElementType!TRange == TStuff))
>      {}
> }
>
> void main(string[] argv)
> {
>      MyClass!int instance = new MyClass!int();
>
>      return;
> }
>
> but if i rename one of the "foo" overloads then code will be compiled.
>

Making the first overload a template fixes your issue, but note that 
your code is indeed valid D code. It is the compiler which does not 
implement overloading of functions vs. function templates yet.
November 17, 2012
Re: template error instantiating
Maxim Fomin wrote:

> related to the issue?

... I can see that the definition is ambiguous. And if the coder 
didnt't realize the ambiguousness as you do ...

-manfred
November 17, 2012
Re: template error instantiating
On 11/18/2012 12:14 AM, Manfred Nowak wrote:
> Maxim Fomin wrote:
>
>> related to the issue?
>
> ... I can see that the definition is ambiguous. And if the coder
> didnt't realize the ambiguousness as you do ...
>
> -manfred
>

The code given in the original post is valid D code.
November 18, 2012
Re: template error instantiating
On Saturday, 17 November 2012 at 23:14:56 UTC, Manfred Nowak 
wrote:
> Maxim Fomin wrote:
>
>> related to the issue?
>
> ... I can see that the definition is ambiguous. And if the coder
> didnt't realize the ambiguousness as you do ...
>
> -manfred

There is compilation error when instantiating class template 
which has on overload set consisting of template and non-template 
function. How this is related to eponymous templates?
November 18, 2012
Re: template error instantiating
On Saturday, 17 November 2012 at 23:28:21 UTC, Timon Gehr wrote:
> On 11/18/2012 12:14 AM, Manfred Nowak wrote:
>> Maxim Fomin wrote:
>>
>>> related to the issue?
>>
>> ... I can see that the definition is ambiguous. And if the 
>> coder
>> didnt't realize the ambiguousness as you do ...
>>
>> -manfred
>>
>
> The code given in the original post is valid D code.

This is a poor argument. Validness of code is determined by the 
spec (and I have not found anything related to the issue) and is 
supported by presence of alternative compilers so you can switch 
to another if you have problems with one of them.

This is not the case of D. Unfortunately D spec is incomplete and 
does not give answers even for not tricky questions. Also, only 
few commits to dmd which affect language have corresponding pulls 
to dlang.org. And presence of alternative compilers does not help 
in majority cases because they share same frontend as well as 
bugs in it. This makes D's bugs to be a chief if you cannot 
fallback to ldc or gdc and makes theoretical validness of code 
irrelevant at practice.
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home