www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Unwanted conflict

reply "Carl Sturtivant" <sturtivant gmail.com> writes:
struct A {
	string s;
	int n;
	this( string s) { this.s = s; }
	this( int k)( int n) { this.n = n - k; }
}

compiled with dmd gives an error message as follows.

constr_conflict.d(5): Error: template 
constr_conflict.A.__ctor(int k)(int n) conflicts with constructor 
constr_conflict.A.this at constr_conflict.d(4)

The exact details seem unimportant except that one constructor 
has compile-time parameters and the other does not.

What is the conflict exactly?
Jul 20 2013
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Carl Sturtivant:

 What is the conflict exactly?
Perhaps it's a bug fixed in GIT head. As workaround try: this()(string s) Bye, bearophile
Jul 20 2013
parent reply "Carl Sturtivant" <sturtivant gmail.com> writes:
On Saturday, 20 July 2013 at 22:33:00 UTC, bearophile wrote:
 Carl Sturtivant:

 What is the conflict exactly?
Perhaps it's a bug fixed in GIT head. As workaround try: this()(string s)
OK, but now I don't know how to call a templated constructor. void main() { A x = A!3(99); } added to the file containing the original class with the workaround edited in doesn't compile, and curiously the error message is produced twice: constr_conflict.d(9): Error: template instance A!(3) A is not a template declaration, it is a struct constr_conflict.d(9): Error: template instance A!(3) A is not a template declaration, it is a struct What do I need to do to make a call of this constructor call compile?
Jul 20 2013
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, July 21, 2013 05:17:03 Carl Sturtivant wrote:
 On Saturday, 20 July 2013 at 22:33:00 UTC, bearophile wrote:
 Carl Sturtivant:
 What is the conflict exactly?
Perhaps it's a bug fixed in GIT head. As workaround try: this()(string s)
OK, but now I don't know how to call a templated constructor. void main() { A x = A!3(99); } added to the file containing the original class with the workaround edited in doesn't compile, and curiously the error message is produced twice: constr_conflict.d(9): Error: template instance A!(3) A is not a template declaration, it is a struct constr_conflict.d(9): Error: template instance A!(3) A is not a template declaration, it is a struct What do I need to do to make a call of this constructor call compile?
Templated constructors will work with IFTI (Implicit Function Template Instantiton - i.e. where the types are inferred from the arguments), but I'm not sure that it works when you have to explicitly instantiate them. I know that if the type itself is templated as wel, then you're out of luck, because the language provides no way to explicitly instantiate both (though maybe you could do it by aliasing the explicitly instantiated templated type and then explicitly instantiating the constructor using the alias). I'd argue that your example should work, because the type itself isn't templated, and the compiler should be able to determine that you're trying to explicitly instantiate the constructor, not the type, but I don't know what the compiler devs' stance on it would be. I'd suggest opening a bug report: http://d.puremagic.com/issues But if your example doesn't work, then I don't know of any to explicitly instantiate a templated constructor. - Jonathan M Davis
Jul 20 2013
parent reply "Carl Sturtivant" <sturtivant gmail.com> writes:
That's too bad, because in the real code this example was derived 
from I can't have type inference call the right constructor as 
there are two constructors with the same run-time argument types.

I'll put in a bug report.
Jul 21 2013
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, July 21, 2013 16:25:15 Carl Sturtivant wrote:
 That's too bad, because in the real code this example was derived
 from I can't have type inference call the right constructor as
 there are two constructors with the same run-time argument types.
You could use a factory function instead. - Jonathan M Davis
Jul 21 2013
parent reply "Carl Sturtivant" <sturtivant gmail.com> writes:
On Sunday, 21 July 2013 at 18:37:23 UTC, Jonathan M Davis wrote:
 On Sunday, July 21, 2013 16:25:15 Carl Sturtivant wrote:
 That's too bad, because in the real code this example was 
 derived
 from I can't have type inference call the right constructor as
 there are two constructors with the same run-time argument 
 types.
You could use a factory function instead. - Jonathan M Davis
Yes, and I suppose it's not that awful of a work-around. Only D invited this nice technique in the interesting situation I am in, and I like simplicity and elegance, and I like to supply nice explanations based thereupon. I do hope this gets to work eventually in a later version. :) Thanks for the input, Carl.
Jul 22 2013
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Tuesday, July 23, 2013 05:42:10 Carl Sturtivant wrote:
 On Sunday, 21 July 2013 at 18:37:23 UTC, Jonathan M Davis wrote:
 On Sunday, July 21, 2013 16:25:15 Carl Sturtivant wrote:
 That's too bad, because in the real code this example was
 derived
 from I can't have type inference call the right constructor as
 there are two constructors with the same run-time argument
 types.
You could use a factory function instead. - Jonathan M Davis
Yes, and I suppose it's not that awful of a work-around. Only D invited this nice technique in the interesting situation I am in, and I like simplicity and elegance, and I like to supply nice explanations based thereupon. I do hope this gets to work eventually in a later version. :)
Well, ideally it would work, and there's a decent chance that it will in the future, but for now, we're out of luck. - Jonathan M Davis
Jul 22 2013
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, July 21, 2013 00:10:40 Carl Sturtivant wrote:
 struct A {
 	string s;
 	int n;
 	this( string s) { this.s = s; }
 	this( int k)( int n) { this.n = n - k; }
 }
 
 compiled with dmd gives an error message as follows.
 
 constr_conflict.d(5): Error: template
 constr_conflict.A.__ctor(int k)(int n) conflicts with constructor
 constr_conflict.A.this at constr_conflict.d(4)
 
 The exact details seem unimportant except that one constructor
 has compile-time parameters and the other does not.
 
 What is the conflict exactly?
As of 2.063, you can't overload a templated function with a non-templated function (or vice versa). That bug has finally been fixed in git master, but it hasn't been released yet. The typical way to workaround the problem is to templatize the non-templated function, which you can do by giving it an empty template parameter list. this()(string S) {...} - Jonathan M Davis
Jul 20 2013