digitalmars.D.learn - passing __FILE__, __MODULE__, etc... with varadic types
- JS (8/8) Jul 19 2013 I would like to pass to all my templates the file and module
- JS (26/34) Jul 19 2013 I should mention, that because file is at the end of the
- Andrej Mitrovic (3/5) Jul 19 2013 Yeah it's a known (and common) issue.
- JS (3/10) Jul 19 2013 It would be nice to get fixed sometime soon but doens't look like
- Jonathan M Davis (9/21) Jul 19 2013 As long as you're using a templated function and thus can use IFTI (impl...
- JS (13/42) Jul 19 2013 I don't at all see how this could possibly work. The order of
- Jonathan M Davis (12/60) Jul 19 2013 As I said, it works if you're using IFTI (Implict Function Template
- JS (12/86) Jul 20 2013 auto unique(T, string file = __FILE__, size_t line = __LINE__)(T
I would like to pass to all my templates the file and module locations where they are used(this goes into a debugging system I have come up with). The problem is, with varadic types being passed I can't do this: template T!(T..., string file = __FILE__) doesn't work. I think there is no way around except to explicitly pass __FILE__... which would be a mess?
Jul 19 2013
On Friday, 19 July 2013 at 09:06:27 UTC, JS wrote:I would like to pass to all my templates the file and module locations where they are used(this goes into a debugging system I have come up with). The problem is, with varadic types being passed I can't do this: template T(T..., string file = __FILE__) doesn't work. I think there is no way around except to explicitly pass __FILE__... which would be a mess?I should mention, that because file is at the end of the parameter list and it will never be supplied by the user, there should be no reason it should fail... I think d should allow such a feature as it is more useful and necessary in some cases... e.g., template A(T..., hidden string file = __FILE__, hidden string mod = __MODULE__) only hidden parameters with default values can be used after a template type tuple. if I try to use a mixin in some way, dmd crashes: import std.stdio, std.cstream; template TT(T...) { enum TT = "T(__FILE__, __MODULE__)"; } template T(string file, string mod, T...) { enum T = file~"\n"~mod; } void main(string[] argv) { writeln(mixin(TT!())); din.getc(); }
Jul 19 2013
On 7/19/13, JS <js.mdnq gmail.com> wrote:The problem is, with varadic types being passed I can't do this: template T!(T..., string file = __FILE__)Yeah it's a known (and common) issue. http://d.puremagic.com/issues/show_bug.cgi?id=8687
Jul 19 2013
On Friday, 19 July 2013 at 11:12:31 UTC, Andrej Mitrovic wrote:On 7/19/13, JS <js.mdnq gmail.com> wrote:It would be nice to get fixed sometime soon but doens't look like that will happen.The problem is, with varadic types being passed I can't do this: template T!(T..., string file = __FILE__)Yeah it's a known (and common) issue. http://d.puremagic.com/issues/show_bug.cgi?id=8687
Jul 19 2013
On Friday, July 19, 2013 11:06:26 JS wrote:I would like to pass to all my templates the file and module locations where they are used(this goes into a debugging system I have come up with). The problem is, with varadic types being passed I can't do this: template T!(T..., string file = __FILE__) doesn't work. I think there is no way around except to explicitly pass __FILE__... which would be a mess?As long as you're using a templated function and thus can use IFTI (implicit function template instantation) instead of giving the template arguments explicitly, you don't have to put the template parameters with default arguments last. You'd just have to put it after the variadic parameter if you intended to give the template arguments explicitly. So, as long as you don't have to give the template arguments explicitly, you're fine. If you need to give them explicitly though, I think that you're stuck. - Jonathan M Davis
Jul 19 2013
On Friday, 19 July 2013 at 15:32:25 UTC, Jonathan M Davis wrote:On Friday, July 19, 2013 11:06:26 JS wrote:I don't at all see how this could possibly work. The order of parameters is crucial. With a type tuple it may be different but I just tried and it didn't work: template A(string f = __FILE__, T...) { pragma(msg, f); enum A = T.stringof; } called with A!(int, double) and got an error about argument type mismatch. So unless you are talking about something else or there is some "trick" involved I don't think this works...I would like to pass to all my templates the file and module locations where they are used(this goes into a debugging system I have come up with). The problem is, with varadic types being passed I can't do this: template T!(T..., string file = __FILE__) doesn't work. I think there is no way around except to explicitly pass __FILE__... which would be a mess?As long as you're using a templated function and thus can use IFTI (implicit function template instantation) instead of giving the template arguments explicitly, you don't have to put the template parameters with default arguments last. You'd just have to put it after the variadic parameter if you intended to give the template arguments explicitly. So, as long as you don't have to give the template arguments explicitly, you're fine. If you need to give them explicitly though, I think that you're stuck. - Jonathan M Davis
Jul 19 2013
On Friday, July 19, 2013 19:23:50 JS wrote:On Friday, 19 July 2013 at 15:32:25 UTC, Jonathan M Davis wrote:As I said, it works if you're using IFTI (Implict Function Template Instantation) but not if you're giving the template arguments explicitly. So, if you're dealing with a templated function such as auto foo(string file = __FILE__, T...)(T args) {...} then it works. But if you're dealing with a template which is not a function (so you have to instantiate it explicitly), or you have to instantiate the templated function explicitly for any reason, then you'll be forced to give the file name as the first argument. So, you have a way out with many templated functions, but for other templates, you still have the problem that you've been running into. - Jonathan M DavisOn Friday, July 19, 2013 11:06:26 JS wrote:I don't at all see how this could possibly work. The order of parameters is crucial. With a type tuple it may be different but I just tried and it didn't work: template A(string f = __FILE__, T...) { pragma(msg, f); enum A = T.stringof; } called with A!(int, double) and got an error about argument type mismatch. So unless you are talking about something else or there is some "trick" involved I don't think this works...I would like to pass to all my templates the file and module locations where they are used(this goes into a debugging system I have come up with). The problem is, with varadic types being passed I can't do this: template T!(T..., string file = __FILE__) doesn't work. I think there is no way around except to explicitly pass __FILE__... which would be a mess?As long as you're using a templated function and thus can use IFTI (implicit function template instantation) instead of giving the template arguments explicitly, you don't have to put the template parameters with default arguments last. You'd just have to put it after the variadic parameter if you intended to give the template arguments explicitly. So, as long as you don't have to give the template arguments explicitly, you're fine. If you need to give them explicitly though, I think that you're stuck. - Jonathan M Davis
Jul 19 2013
On Friday, 19 July 2013 at 17:51:29 UTC, Jonathan M Davis wrote:On Friday, July 19, 2013 19:23:50 JS wrote:auto unique(T, string file = __FILE__, size_t line = __LINE__)(T t) { return Unique!(T, file, line)(t); } Thanks, I just came across https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/D-templates-tutorial.pdf which has a lot of good info... I guess I didn't understand ITFI at first... seems pretty powerful. I think I can use such a function to solve the original problem I had. Thanks again.On Friday, 19 July 2013 at 15:32:25 UTC, Jonathan M Davis wrote:As I said, it works if you're using IFTI (Implict Function Template Instantation) but not if you're giving the template arguments explicitly. So, if you're dealing with a templated function such as auto foo(string file = __FILE__, T...)(T args) {...} then it works. But if you're dealing with a template which is not a function (so you have to instantiate it explicitly), or you have to instantiate the templated function explicitly for any reason, then you'll be forced to give the file name as the first argument. So, you have a way out with many templated functions, but for other templates, you still have the problem that you've been running into. - Jonathan M DavisOn Friday, July 19, 2013 11:06:26 JS wrote:I don't at all see how this could possibly work. The order of parameters is crucial. With a type tuple it may be different but I just tried and it didn't work: template A(string f = __FILE__, T...) { pragma(msg, f); enum A = T.stringof; } called with A!(int, double) and got an error about argument type mismatch. So unless you are talking about something else or there is some "trick" involved I don't think this works...I would like to pass to all my templates the file and module locations where they are used(this goes into a debugging system I have come up with). The problem is, with varadic types being passed I can't do this: template T!(T..., string file = __FILE__) doesn't work. I think there is no way around except to explicitly pass __FILE__... which would be a mess?As long as you're using a templated function and thus can use IFTI (implicit function template instantation) instead of giving the template arguments explicitly, you don't have to put the template parameters with default arguments last. You'd just have to put it after the variadic parameter if you intended to give the template arguments explicitly. So, as long as you don't have to give the template arguments explicitly, you're fine. If you need to give them explicitly though, I think that you're stuck. - Jonathan M Davis
Jul 20 2013