passing __FILE__, __MODULE__, etc... with varadic types

JS js.mdnq at gmail.com
Sat Jul 20 04:45:17 PDT 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:
>> 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.


More information about the Digitalmars-d-learn mailing list