digitalmars.D.learn - Unwanted conflict
- Carl Sturtivant (13/13) Jul 20 2013 struct A {
- bearophile (5/6) Jul 20 2013 Perhaps it's a bug fixed in GIT head. As workaround try:
- Carl Sturtivant (14/18) Jul 20 2013 OK, but now I don't know how to call a templated constructor.
- Jonathan M Davis (16/41) Jul 20 2013 Templated constructors will work with IFTI (Implicit Function Template
- Carl Sturtivant (4/4) Jul 21 2013 That's too bad, because in the real code this example was derived
- Jonathan M Davis (3/6) Jul 21 2013 You could use a factory function instead.
- Carl Sturtivant (8/16) Jul 22 2013 Yes, and I suppose it's not that awful of a work-around. Only D
- Jonathan M Davis (4/21) Jul 22 2013 Well, ideally it would work, and there's a decent chance that it will in...
- Jonathan M Davis (8/25) Jul 20 2013 As of 2.063, you can't overload a templated function with a non-template...
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
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
On Saturday, 20 July 2013 at 22:33:00 UTC, bearophile wrote:Carl Sturtivant: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?What is the conflict exactly?Perhaps it's a bug fixed in GIT head. As workaround try: this()(string s)
Jul 20 2013
On Sunday, July 21, 2013 05:17:03 Carl Sturtivant wrote:On Saturday, 20 July 2013 at 22:33:00 UTC, bearophile wrote: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 DavisCarl Sturtivant: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?What is the conflict exactly?Perhaps it's a bug fixed in GIT head. As workaround try: this()(string s)
Jul 20 2013
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
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
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: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.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 22 2013
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: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 DavisOn Sunday, July 21, 2013 16:25:15 Carl Sturtivant wrote: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. :)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 22 2013
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