digitalmars.D.bugs - [Issue 337] New: IFTI and overloading are incompatible
- d-bugmail puremagic.com (67/67) Sep 10 2006 http://d.puremagic.com/issues/show_bug.cgi?id=337
- d-bugmail puremagic.com (48/48) Sep 26 2006 http://d.puremagic.com/issues/show_bug.cgi?id=337
- Sean Kelly (5/9) Oct 25 2006 I had no idea this was legal--it isn't in C++. I guess the rationale is...
- Bruno Medeiros (18/30) Nov 01 2006 Well, I've never actually learned C++ templates[*], so I my thinking
- Sean Kelly (6/28) Nov 01 2006 In C++, template parameter defaults work just like function parameter
- d-bugmail puremagic.com (10/10) Oct 25 2006 http://d.puremagic.com/issues/show_bug.cgi?id=337
- d-bugmail puremagic.com (7/10) Dec 17 2006 http://d.puremagic.com/issues/show_bug.cgi?id=337
- d-bugmail puremagic.com (9/9) Jun 11 2008 http://d.puremagic.com/issues/show_bug.cgi?id=337
- d-bugmail puremagic.com (10/10) Jun 13 2008 http://d.puremagic.com/issues/show_bug.cgi?id=337
- d-bugmail puremagic.com (16/16) Jan 01 2012 http://d.puremagic.com/issues/show_bug.cgi?id=337
http://d.puremagic.com/issues/show_bug.cgi?id=337 Summary: IFTI and overloading are incompatible Product: D Version: 0.166 Platform: All OS/Version: All Status: NEW Severity: major Priority: P2 Component: DMD AssignedTo: bugzilla digitalmars.com ReportedBy: mclysenk mtu.edu Two overloaded template functions with the same template signature will cause a name conflict. Here is an example: //---- import std.stdio; void testfunc(T)(T a) { writefln("a = %s", a); } void testfunc(T)(int x, T a) { writefln("x = %d, a = %s", x, a); } void main() { testfunc("test1"); testfunc(10, "test2"); } //---- In this situation there is a name conflict between the two overloads of testfunc since their templates have the exact same signature. One solution is to wrap them both inside a single template as follows: //---- template testfunc(T) { void testfunc(T a) { ... } void testfunc(int x, T a) { ... } } //---- However, testfunc is no longer considered a function template after such an operation, and IFTI is no longer applied. This approach also prevents other modules from performing an overload on testfunc, such as declaring something like: //---- void testfunc(T)(int x, int y, T a) { writefln("x = %d, y = %d, a = %s", x, y, a); } //---- This will result in a name conflict with the previous declarations. One solution is to add all of the types in testfunc to the template specification like this: //---- void testfunc(Tx : int, Ta)(Tx x, Ta a) { writefln("x = %d, a = %s", x, a); } //---- Unfortunately this gives the error: modname.d(xx): template modname.testfunc(Tx : int,Ta) specialization not allowed for deduced parameter Tx The situation becomes even worse if the type of Tx is deduced from Ta, such as a delegate or another template instance: void foreach_wrapper(T)(T[] a, void delegate(inout T) dg); void foreach_wrapper(T)(T[] a, void delegate(int, inout T) dg); --
Sep 10 2006
http://d.puremagic.com/issues/show_bug.cgi?id=337 brunodomedeiros+bugz gmail.com changed: What |Removed |Added ---------------------------------------------------------------------------- Summary|IFTI and overloading are |Function Templates cannot be |incompatible |overloaded Saying this is IFTI related isn't exactly accurate. Since the problem occurs at template definition, no instantiation is need (implicit or explicit). I'm not sure this is a bug (an enhancement perhaps). The spec is not explicit, but only functions can be overloaded, or templates with different specializations. Templated functions are templates foremost (not functions), so they can only be overloaded if the specialization is different, which is not the case. However I think the following workarounds work (without loss of functionality) : * For overload based on number of parameters only: -------- import std.stdio; void testfunc(T)(T a) { writefln("a = %s", a); } void testfunc(DUMMY = void, T)(int x, T a) { writefln("x = %d, a = %s", x, a); } void main() { testfunc("test1"); testfunc(10, "test2"); } ---- * For any kind of overload (just make dummy specializations): -------- void foreach_wrapper(T)(T[] a, void delegate(inout T) dg) { } void foreach_wrapper(DUMMY : int=int, T)(T[] a, void delegate(int, T) dg) { pragma(msg, "Second specialization"); } void foreach_wrapper(DUMMY : char=char, T)(T[] a, void delegate(char, T) dg) { pragma(msg, "Third specialization"); } void main() { void delegate(int, inout byte) dg1; foreach_wrapper(new byte[3], dg1); void delegate(char, inout byte) dg2; foreach_wrapper(new byte[3], dg2); } ---- --
Sep 26 2006
d-bugmail puremagic.com wrote:void testfunc(DUMMY = void, T)(int x, T a) { writefln("x = %d, a = %s", x, a); }I had no idea this was legal--it isn't in C++. I guess the rationale is that if the compiler can figure out the remaining parameters by inspecting function arguments then this is not an error? Sean
Oct 25 2006
Sean Kelly wrote:d-bugmail puremagic.com wrote:Well, I've never actually learned C++ templates[*], so I my thinking isn't bound to what C++ can or cannot do :P . Anyway, I'm not sure I understand your question. At the time I just checked the docs to see what could be done with IFTI, and it was stated that: "Template arguments not implicitly deduced can have default values: void Foo(T, U=T*)(T t) { U p; ... } " So in both explicit and implicit instantiation you can also have parameters that are deduced from other parameters. (Not the case in C++ then?) The DUMMY case above is just a case where such deduction is constant and not actually dependent on any other parameters. [*] Well, I did a learn some of C++ templates, but only the basics, and only after learning D's and also usually under the perspective of D comparison. -- Bruno Medeiros - MSc in CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#Dvoid testfunc(DUMMY = void, T)(int x, T a) { writefln("x = %d, a = %s", x, a); }I had no idea this was legal--it isn't in C++. I guess the rationale is that if the compiler can figure out the remaining parameters by inspecting function arguments then this is not an error? Sean
Nov 01 2006
Bruno Medeiros wrote:Sean Kelly wrote:In C++, template parameter defaults work just like function parameter defaults--they can only be used for the final N parameters. Also, defaults can only be used for classes in C++, not functions. But I really like the way D works here instead. Seand-bugmail puremagic.com wrote:Well, I've never actually learned C++ templates[*], so I my thinking isn't bound to what C++ can or cannot do :P . Anyway, I'm not sure I understand your question. At the time I just checked the docs to see what could be done with IFTI, and it was stated that: "Template arguments not implicitly deduced can have default values: void Foo(T, U=T*)(T t) { U p; ... } " So in both explicit and implicit instantiation you can also have parameters that are deduced from other parameters. (Not the case in C++ then?) The DUMMY case above is just a case where such deduction is constant and not actually dependent on any other parameters.void testfunc(DUMMY = void, T)(int x, T a) { writefln("x = %d, a = %s", x, a); }I had no idea this was legal--it isn't in C++. I guess the rationale is that if the compiler can figure out the remaining parameters by inspecting function arguments then this is not an error?
Nov 01 2006
http://d.puremagic.com/issues/show_bug.cgi?id=337 bugzilla digitalmars.com changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED Resolution| |WONTFIX I agree with Bruno. While it isn't pretty, the workarounds he presented get us past the problem. --
Oct 25 2006
http://d.puremagic.com/issues/show_bug.cgi?id=337I agree with Bruno. While it isn't pretty, the workarounds he presented get us past the problem.So why can't this sort of workaround be applied automatically by the compiler. The need to stick a dummy parameter in to work around the inability to overload templates is about as FAR from obvious as it gets. --
Dec 17 2006
http://d.puremagic.com/issues/show_bug.cgi?id=337 larsivar igesund.net changed: What |Removed |Added ---------------------------------------------------------------------------- Status|RESOLVED |REOPENED Resolution|WONTFIX | IFTI is next to useless without properly working overload of ifti functions. --
Jun 11 2008
http://d.puremagic.com/issues/show_bug.cgi?id=337 //---- void testfunc(Tx : int, Ta)(Tx x, Ta a) { writefln("x = %d, a = %s", x, a); } //---- works with current versions of both D1.0 and D2.0. --
Jun 13 2008
http://d.puremagic.com/issues/show_bug.cgi?id=337 Stewart Gordon <smjg iname.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Keywords| |spec CC| |smjg iname.com Summary|Function Templates cannot |Function templates cannot |be overloaded |be overloaded Severity|major |enhancement I agree with the analysis - two templates can't have the same signature. So it would be a matter of changing the language to allow it if they can be instantiated in a way that disambiguates them. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 01 2012