www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - passing __FILE__, __MODULE__, etc... with varadic types

reply "JS" <js.mdnq gmail.com> writes:
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
next sibling parent "JS" <js.mdnq gmail.com> writes:
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
prev sibling next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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
parent "JS" <js.mdnq gmail.com> writes:
On Friday, 19 July 2013 at 11:12:31 UTC, Andrej Mitrovic wrote:
 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
It would be nice to get fixed sometime soon but doens't look like that will happen.
Jul 19 2013
prev sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
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
parent reply "JS" <js.mdnq gmail.com> writes:
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 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
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...
Jul 19 2013
parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Friday, July 19, 2013 19:23:50 JS wrote:
 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 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
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...
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 Davis
Jul 19 2013
parent "JS" <js.mdnq gmail.com> writes:
On Friday, 19 July 2013 at 17:51:29 UTC, Jonathan M Davis wrote:
 On Friday, July 19, 2013 19:23:50 JS wrote:
 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 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
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...
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 Davis
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.
Jul 20 2013