digitalmars.D.learn - Cannot use the same template arguments on function as the ones on
- MrSmith (35/35) Feb 11 2015 Here I have templated struct that matches type with CborConfig
- anonymous (7/11) Feb 11 2015 Seems to work when you add an empty template argument list to
- Kenji Hara (6/20) Feb 12 2015 It would be a compiler bug. As far as I know, accepter() and
- MrSmith (1/1) Feb 12 2015 Thank you!
- Stefan Koch (2/2) Feb 11 2015 DMD cannot overload templated and non-templated functions
- Jonathan M Davis via Digitalmars-d-learn (14/16) Feb 11 2015 That's not actually true anymore. However, you have to be really careful
- MrSmith (1/1) Feb 12 2015 Thanks, everyone.
Here I have templated struct that matches type with CborConfig tempate specialization CborConfig will have more parameters in future and all of them will be accessed via alias members, so I've used variadic (T...) parameter whule matching. ------- template CborConfig(_nonSerializedAttribute) { struct CborConfig { alias nonSerializedAttribute = _nonSerializedAttribute; } } /// Default non-serialized attribute type struct NonSerialized{} /// This CborConfig instantiation will be used by default. alias defaultCborConfig = CborConfig!(NonSerialized, NonSerialized); struct AccepterT(Config : CborConfig!(T) = defaultCborConfig, T...) { pragma(msg, T); } // template f379.accepter cannot deduce function from argument types !()() void accepter(Config : CborConfig!(T) = defaultCborConfig, T...)() { pragma(msg, T); } ------- ^^^^^^^ http://dpaste.dzfl.pl/5f1d5d5d9e19 Instead I need to use template constraint which is less compact. http://dpaste.dzfl.pl/571ae84d783e Why such behavior happens?
Feb 11 2015
On Wednesday, 11 February 2015 at 22:14:44 UTC, MrSmith wrote:http://dpaste.dzfl.pl/5f1d5d5d9e19 Instead I need to use template constraint which is less compact. http://dpaste.dzfl.pl/571ae84d783e Why such behavior happens?Seems to work when you add an empty template argument list to `accepter()`, making it `accepter!()()`: http://dpaste.dzfl.pl/2ec186453907 So, compiler bug, I guess? The error message says it tried "!()()", but explicit "!()()" actually works. And the empty template argument list should be optional anyway.
Feb 11 2015
On Wednesday, 11 February 2015 at 22:24:53 UTC, anonymous wrote:On Wednesday, 11 February 2015 at 22:14:44 UTC, MrSmith wrote:It would be a compiler bug. As far as I know, accepter() and accepter!()() has no difference in IFTI. I filed the issue in bugzilla. https://issues.dlang.org/show_bug.cgi?id=14174 Kenji Harahttp://dpaste.dzfl.pl/5f1d5d5d9e19 Instead I need to use template constraint which is less compact. http://dpaste.dzfl.pl/571ae84d783e Why such behavior happens?Seems to work when you add an empty template argument list to `accepter()`, making it `accepter!()()`: http://dpaste.dzfl.pl/2ec186453907 So, compiler bug, I guess? The error message says it tried "!()()", but explicit "!()()" actually works. And the empty template argument list should be optional anyway.
Feb 12 2015
DMD cannot overload templated and non-templated functions an empty template argument is needed
Feb 11 2015
On Thursday, February 12, 2015 07:11:12 Stefan Koch via Digitalmars-d-learn wrote:DMD cannot overload templated and non-templated functions an empty template argument is neededThat's not actually true anymore. However, you have to be really careful when you do it, because it pretty much always favors calling the non-templated overload when it can, so you need to be very sure that the right overloads are getting called with the various types. Ultimately, I think that it's just better to templatize all of the functions and then get the constraints right, but if the set of types that are supposed to use the templated overload aren't even vaguely related to the non-templated overload, then you're probably okay. However, I've found that stuff like constness and inheritance tends to make it so that the non-templated overload ends up being called when I want the templated one to be called. So, in the end, it just pays to test and make sure that the right overload gets called for the various types that are supposed to be used with them. - Jonathan M Davis
Feb 11 2015